# Ruby Shortcut to Call a Method with Each Array Item as Parameter

*Published on June 10, 2022*

---


As a Ruby programmer, you probably know the&nbsp;`&:`&nbsp;shortcut to call a method on each item in an array. For example, this snippet will call the&nbsp;`to_s`&nbsp;method on&nbsp;`:foo`&nbsp;and&nbsp;`:bar`.

```ruby
[:foo, :bar].each(&:to_s)
```

The above code is similar to:

```ruby
[:foo, :bar].each do |item|
  item.to_s
end
```

However, instead of calling a method on the array item, you want to&nbsp;**call a method and pass the array item to that method.**&nbsp;Is there a one-liner for that?

```javascript
[:foo, :bar].each do |item|
  puts(item)
end
```

It turns out, Ruby has a corresponding&nbsp;`&method`&nbsp;shortcut to accomplish precisely this.

![method_shortcut.webp](https://writesoftwarewell.com/media/zigzhpec3edq66xfm09e6vy8puav.webp "Method Shortcut")

Two things are going on here.

First, the&nbsp;`method(:puts)`&nbsp;finds the method named&nbsp;`puts`. As per the documentation, you can access the instance variables and&nbsp;`self`&nbsp;method on whichever object you called&nbsp;`method`&nbsp;on.

> [_method(sym) -\> method_](https://ruby-doc.org/core-3.1.1/Object.html#method-i-method)  
>   
> _Looks up the named method as a receiver in obj, returning a_&nbsp;[_`Method`_](https://ruby-doc.org/core-3.1.1/Method.html)&nbsp;_object (or raising_&nbsp;[_`NameError`_](https://ruby-doc.org/core-3.1.1/NameError.html)_). The_&nbsp;[_`Method`_](https://ruby-doc.org/core-3.1.1/Method.html)&nbsp;_object acts as a closure in obj’s object instance, so instance variables and the value of_&nbsp;_`self`_&nbsp;_remain available._

Second, the&nbsp;`Method`&nbsp;object returned by the&nbsp;`method`&nbsp;method (_wow, that’s one too many methods_) implements the&nbsp;`to_proc`&nbsp;method, so you can use it after&nbsp;`&`, which is to say:

> If&nbsp;`foo`&nbsp;is an object with a&nbsp;`to_proc`&nbsp;method, then you can pass it to a method as&nbsp;`&foo`, which will call&nbsp;`foo.to_proc`&nbsp;and use that as the method’s block.

```ruby
&method(:puts) # is equivalent to
{ |e| puts(e) }

&:name # is equivalent to
{ |e| e.name }
```

When you say&nbsp;`&method(:puts)`, it finds the&nbsp;`Method`&nbsp;object and calls&nbsp;`.to_proc`&nbsp;on it.

```ruby
array.each(&method(:foo))
```

is equivalent to,

```ruby
array.each { |e| foo(e) }
```

* * *

## **Is It Worth It?**

Now, does that mean you go ahead and replace the explicit method calls in&nbsp;`each`&nbsp;loops with this one-liner?&nbsp;**Probably not.**

Sometimes, that explicit method call is just what you need to keep the code readable and easier to understand. Not just for your colleagues, but for the future you. Using this one-liner adds an extra mental overhead for your brain to translate and understand what it’s doing, which is not worth in many cases.

However, sometimes this shortcut is precisely what you need to express what you meant. That’s why I love Ruby. The freedom to choose, do, and say whatever you want, however you want, to write software, well.

Anyway, hope that helps.

