DEV Community

Cover image for Discoveries in Ruby(and Rails): Array#*
Ahmad khattab
Ahmad khattab

Posted on

Discoveries in Ruby(and Rails): Array#*

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 "-"
Enter fullscreen mode Exit fullscreen mode

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"] * "-"
Enter fullscreen mode Exit fullscreen mode

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"] * "-"
Enter fullscreen mode Exit fullscreen mode

Thank you for reading. Happy Coding!.

References

Top comments (0)