Array¶
Rice::Array wraps Ruby's Array class and provides a C++-style interface with STL-compatible iterators.
Constructors¶
Array()¶
Construct a new empty array.
Array(long capacity)¶
Construct a new array with pre-allocated capacity.
Parameters:
capacity- The initial capacity.
Array(VALUE v)¶
Wrap an existing Ruby array VALUE.
Parameters:
v- A Ruby VALUE of typeT_ARRAY.
Array(Object v)¶
Wrap an existing Ruby Object that is an array.
Parameters:
v- An Object wrapping an array VALUE.
Array(Iter_T begin, Iter_T end)¶
Construct an array from an iterator range.
Template Parameters:
Iter_T- The iterator type.
Parameters:
begin- Iterator to the start of the range.end- Iterator to the end of the range.
Array(T (&a)[n])¶
Construct an array from a C array.
Template Parameters:
T- The element type.n- The array size.
Parameters:
a- A C array.
Public Methods¶
size() const → long¶
Get the number of elements in the array.
Returns:
The array size.
operator[](long index) const → Object¶
Get the element at the given index (read-only).
Parameters:
index- The index (may be negative to count from end).
Returns:
The element as an Object.
operator[](long index) → Proxy¶
Get a proxy for the element at the given index (read-write).
Parameters:
index- The index (may be negative).
Returns:
A Proxy that allows assignment.
push(T&& obj, bool takeOwnership = false) → Object¶
Append an element to the end of the array.
Parameters:
obj- The object to append (automatically converted to Ruby).takeOwnership- If true, Ruby takes ownership of wrapped C++ objects.
Returns:
The appended object.
pop() → Object¶
Remove and return the last element.
Returns:
The removed element, or Qnil if empty.
unshift(T const& obj) → Object¶
Prepend an element to the beginning of the array.
Parameters:
obj- The object to prepend.
Returns:
The prepended object.
shift() → Object¶
Remove and return the first element.
Returns:
The removed element.
join(const char* separator) → String¶
Join array elements into a string.
Parameters:
separator- The separator between elements.
Returns:
A String with joined elements.
Array a;
a.push("a", false);
a.push("b", false);
a.push("c", false);
String s = a.join(", "); // "a, b, c"
pack() → String¶
Pack array elements into a binary string.
Template Parameters:
T- The type to pack as.
Returns:
A String containing packed binary data.
to_vector() → std::vector¶
Convert the array to a C++ vector.
Template Parameters:
T- The element type.
Returns:
A std::vector containing converted elements.
Array a;
a.push(1, false);
a.push(2, false);
a.push(3, false);
std::vector<int> vec = a.to_vector<int>();
Iterators¶
Array provides STL-compatible random-access iterators.
Types¶
Array::iterator- Mutable iteratorArray::const_iterator- Const iterator
Methods¶
begin() → iterator¶
Return an iterator to the first element.
end() → iterator¶
Return an iterator past the last element.
begin() const → const_iterator¶
Return a const iterator to the first element.
end() const → const_iterator¶
Return a const iterator past the last element.
Example¶
Array a;
a.push(1, false);
a.push(2, false);
a.push(3, false);
// Range-based for loop
for (auto elem : a) {
std::cout << elem << std::endl;
}
// Iterator-based loop
for (auto it = a.begin(); it != a.end(); ++it) {
std::cout << *it << std::endl;
}
// Random access
auto it = a.begin();
it += 2; // Jump to third element
std::cout << *it << std::endl; // 3
// STL algorithms work with const_iterators
auto dist = std::distance(a.begin(), a.end()); // 3
Inherited Methods¶
Array inherits all methods from Object, including:
value()- Get the underlying VALUEcall()- Call Ruby methods likesort,map, etc.is_nil()- Check if nil
Example¶
#include <rice/rice.hpp>
#include <iostream>
#include <algorithm>
using namespace Rice;
void example()
{
// Create and populate array
Array a;
a.push(3, false);
a.push(1, false);
a.push(4, false);
a.push(1, false);
a.push(5, false);
// Size
std::cout << "Size: " << a.size() << std::endl; // 5
// Access elements
std::cout << "First: " << a[0] << std::endl; // 3
std::cout << "Last: " << a[-1] << std::endl; // 5
// Modify element
a[0] = 10;
// Iterate
std::cout << "Elements: ";
for (auto elem : a) {
std::cout << elem << " ";
}
std::cout << std::endl;
// Use Ruby methods
Object sorted = a.call("sort");
Object sum = a.call("sum");
// Convert to C++
std::vector<int> vec = a.to_vector<int>();
// Push/pop
a.push(9, false);
Object popped = a.pop(); // 9
// Join
Array strings;
strings.push("a", false);
strings.push("b", false);
String joined = strings.join("-"); // "a-b"
}