Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Array#<<
(Elem object) -> self
self << object → self
array.c
View on GitHub ↗
VALUE
rb_ary_push(VALUE ary, VALUE item)
{
rb_len_t idx = RARRAY_LEN((ary_verify(ary), ary));
VALUE target_ary = ary_ensure_room_for_push(ary, 1);
RARRAY_PTR_USE(ary, ptr, {
RB_OBJ_WRITE(target_ary, &ptr[idx], item);
});
ARY_SET_LEN(ary, idx + 1);
ary_verify(ary);
return ary;
}
Appends object as the last element in self; returns self:
Example
[:foo, 'bar', 2] << :baz # => [:foo, "bar", 2, :baz]
Appends object as a single element, even if it is another array:
Example
[:foo, 'bar', 2] << [3, 4] # => [:foo, "bar", 2, [3, 4]]
Related: see Methods for Assigning.