Ruby Tuesday – Array#join

Today’s Ruby Tuesday is on Array#join

["Now", "is", "the", "time", "for", "all", "good", "people"].join(" ")
# "Now is the time for all good people"
["1", "2", "3", "4", "5"].join("x")
# "1x2x3x4x5"

If the array contains items that are not strings, they will be coerced to strings and then joined together.

[1, 2, 3, 4, 5].join(" ")
"1 2 3 4 5"
[1, 2, 3, 4, 5].join("x")
# "1x2x3x4x5"
[1, 2, 3, 4, 5].join
# "12345"

If no argument is passed to Array#join, it uses the value set in $,.

$, = "n"
# "n"
[1, 2, 3, 4, 5].join
# "1n2n3n4n5"

–Proctor

Leave a Reply

Your email address will not be published. Required fields are marked *