Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby array shortcuts - "&:" and "&method" #5

Open
nvtin opened this issue Dec 6, 2016 · 0 comments
Open

Ruby array shortcuts - "&:" and "&method" #5

nvtin opened this issue Dec 6, 2016 · 0 comments

Comments

@nvtin
Copy link
Owner

nvtin commented Dec 6, 2016

Call a method on every items with &:
So this

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

we can reduce to
[:foo, :bar].each(&:to_s)

But, what if you want to call a method for each item in an array, and this item should be a parameter for this method?

Call a method with every items as a parameter with &method

So this:

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

Can be reduce to
[:foo, :bar].each(&method(:puts))

Source: https://til.hashrocket.com/posts/2dab9b4db4-ruby-array-shortcuts-and-method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant