In Ruby, you can append a value to an existing array by using the
Array#push method, for example, like so:
a = ["foo", "bar"] a.push("baz") print a #=> ["foo", "bar", "baz"]
Starting with Ruby 2.5+, you may also use the
Array#append method, which is an alias for
As an alternative, you may use the
Array#insert method to add a value to the end of an array, for example, like so:
a = ["foo", "bar"] a.insert(-1, "baz") print a #=> ["foo", "bar", "baz"]
Using any of these methods would do in-place modification of the array (as opposed to returning a new one) — which means that it would mutate/modify the original array.
Hope you found this post useful. It was published . Please show your love and support by sharing this post.