Ruby Array : Concatenation

Arrays Concatenation 

Approach 1
chars1 = ["a", "b", "c"]
chars2 = ["d", "e", "f", "g"]
chars = chars1 + chars2
=> ["a", "b", "c", "d", "e", "f", "g"]
Approach 2

chars1 = ["a", "b", "c"]
chars2 = ["d", "e", "f", "g"]
days = chars1.concat(chars2)

=> ["a", "b", "c", "d", "e", "f", "g"]

Approach 3

chars = ["a", "b", "c"]
chars << "d" << "e" << "f" << "g"
=> ["a", "b", "c", "d", "e", "f", "g"]

Comments

Popular posts from this blog

Inserting and Moving elements inside Ruby Array

Interview Question on Ruby on Rails