End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Array#<<
array << object → self
array.c
View on GitHub ↗
VALUE
rb_ary_push(VALUE ary, VALUE item)
{
long idx = RARRAY_LEN((ary_verify(ary), ary));
VALUE target_ary = ary_ensure_room_for_push(ary, 1);
RARRAY_PTR_USE_TRANSIENT(ary, ptr, {
RB_OBJ_WRITE(target_ary, &ptr[idx], item);
});
ARY_SET_LEN(ary, idx + 1);
ary_verify(ary);
return ary;
}
Appends object to self; returns self:
Example
a = [:foo, 'bar', 2] a << :baz # => [:foo, "bar", 2, :baz]
Appends object as one element, even if it is another Array:
Example
a = [:foo, 'bar', 2] a1 = a << [3, 4] a1 # => [:foo, "bar", 2, [3, 4]]