This blog is part of our Ruby 2.5 series.
Ruby has Array#unshift to prepend an element to the start of an array and
Array#push to append an element to the end of an array.
The names of these methods are not very intuitive. Active Support from Rails
already has aliases for
the unshift and push methods
, namely prepend and append.
In Ruby 2.5, these methods are added in the Ruby language itself.
>> a = ["hello"]
=> ["hello"]
>> a.append "world"
=> ["hello", "world"]
>> a.prepend "Hey"
=> ["Hey", "hello", "world"]
>>
They are implemented as aliases to the original unshift and push methods so
there is no change in the behavior.

Written byPrathamesh Sonpatki
Related blogs
- 01Ruby 2.5 added lazy proc allocation for block parametersMay 22, 2018 · Amit Choudhary
- 02Ruby 2.5 adds Thread.report_on_exception by defaultApril 18, 2018 · Vishal Telangre
- 03Ruby 2.5 adds Exception#full_message methodMarch 13, 2018 · Vishal Telangre
- 04Ruby 2.5 prints backtrace & error message in reverseMarch 7, 2018 · Vishal Telangre
Follow @bigbinary on X. Check out our full blog archive.