Back to Blog

Array#prepend and Array#append in Ruby 2.5

on December 19, 2017
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.

1>> a = ["hello"]
2=> ["hello"]
3>> a.append "world"
4=> ["hello", "world"]
5>> a.prepend "Hey"
6=> ["Hey", "hello", "world"]

They are implemented as aliases to the original unshift and push methods so there is no change in the behavior.