Skip to content

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

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.

See also #- #<=> #+ #== also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0