DEV Community

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

Posted on

2 1

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️