~/adventure-log/2024/11/passing-block-arguments-in-ruby
Tech

Passing block arguments in Ruby

A quick look at how the `&` operator works in Ruby, from `Symbol#to_proc` to converting method objects into blocks.

Published
Updated
Reading time
2 min
Tags
RubyTips

in Ruby, we can pass block argument to an function by prefixing with an &. let's see it with examples.

Symbol

a common example of the use of objects that implement to_proc in Symbol.

ruby
["a", "b", "c"].map { |s| s.upcase } # => ["A", "B", "C"]
["a", "b", "c"].map(&:upcase)        # => ["A", "B", "C"]

these two lines of code behave identically.

the reason this works is that Symbol implements the to_proc method. all it does was *take the argument to this proc, and call the method whose name maches this symbol.*

we'll frequently see this syntax as a shortcut fot methods that take simple blocks like map or sort_by

Make it simpler

we can pass usual do blocks to function, but some times we can make it more simple. let's see an example.

ruby
numbers = 1..5

operator = gets

number = Integer(gets)

if operator.start_with?("t")
  puts numbers.collect { |n| n * number }.join(", ")
else
  puts numbers.collect { |n| n + number }.join(", ")
end

this code works, but ugly. we can refactor it like this below.

ruby
numbers = 1..5

operator = gets

number = Integer(gets)

if operator.start_with?("t")
  calc = ->(n) { n * number }
else
  calc = ->(n) { n + number }
end

puts numbers.map(&calc).join(", ")

in this version, we assign the correct block to a variable named calc, then we pass calc to the method map, prefixing it with &

but there's a even shorter way to write this code. Ruby object have a method name called `method`. which takes a symbol and returns the object's method of the same name.

ruby
operators = get

number = Integer(gets)

method = number.method(operator.start_with?("t") ? :* : :+)

puts (1..5).map(&method).join(", ")

In this case, we’re using method to grab the method named :+ or :* based on the input and using the ampersand’s to_proc powers to create a proc that calls that method.

You got a collectible· Item get

No.002

Ancient Wheel

Common

Region
Tech Highlands
Found near
Ruby · Tips
Discovered
Journey
2 min

Earned by finishing this quest. The more travellers who read it, the rarer it grows.

Epona
Written byEpona

There's nothing wrong with having a little fun

x.com/simura_epona

Loading comments…