DEV Community

Augusts Bautra
Augusts Bautra

Posted on

Idiomatic shorthand loops in Ruby

Ruby has some of the sleekest looping idioms in all of coding, and with some idiomatic sugar, you can make them even more compact.

TL;DR - numbered block arguments are pretty good.

1. Call some method on each element

[1, 2].map(&:to_s)     #=> ["1", "2"]
[1, 2].map { _1.to_s } #=> ["1", "2"]
Enter fullscreen mode Exit fullscreen mode

2. Call some method with each element as the argument

def greet(object, volume)
  "Hi, #{object}, #{volume}"
end

guests = [[1, :loudly], [2, :quietly]]

guests.map { |args| greet(*args) }
#=> ["Hi, 1, loudly", "Hi, 2, quietly"]
guests.map { greet(_1, _2) }
#=> ["Hi, 1, loudly", "Hi, 2, quietly"]
Enter fullscreen mode Exit fullscreen mode

Kwargs work with numbered args quite well also:

def greet(name:)
  "Hi, #{name}"
end

guests = [{name: "Bob"}, {name: "Mary"}]

guests.map { |kwargs| greet(**kwargs) }
#=> ["Hi, Bob", "Hi, Mary"] 
guests.map { greet(**_1) }
#=> ["Hi, Bob", "Hi, Mary"] 
Enter fullscreen mode Exit fullscreen mode

2-b. (cryptic edition, don't use this!) Call some method with each element.

def greet(object)
  "Hi, #{object}"
end

[1, 2].map(&method(:greet))
#=> nil # some weirdness with inline return 
x = [1, 2].map(&method(:greet))
#=> ["Hi, 1", "Hi, 2"] # works with assignment, phew
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

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.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay