3

I want to send an method to each object in an Array. I know I can do something like this

array = ...
array.each { |obj| obj.some_method }

But is there a method where I can do something like the following?

array = ...
array.send_each :some_method

1 Answer 1

7

Use Symbol#to_proc:

array.each(&:some_method)
Sign up to request clarification or add additional context in comments.

2 Comments

Which is shorthand for array.each{ |object| object.some_method }. Use that version if you need to pass parameters to the method.
@Thilo That's pretty redundant, given that's the exact code posted in the question... Also not correct. It's short-hand for array.each(&Proc.new { |x| x.some_method }).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.