Sometimes when you need to join the elements of an array you would end up calling Array#join and pass the argument to the method.
["one", "two", "three"].join "-"
If we run the code above in an irb session. You effectively get one-two-three
returned.
Ruby provides various sets of syntactical sugar. Another example in Ruby's elegancy is that you can also do the same operation as join by calling Array#* method.
["one", "two", "three"] * "-"
The result from running this snippet is the same as the first code. To confirm this, run the following code in an irb session. The result returned to you should be true.
["one", "two", "three"].join("-") == ["one", "two", "three"] * "-"
Thank you for reading. Happy Coding!.
Top comments (0)