We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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)
[: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))
[:foo, :bar].each(&method(:puts))
Source: https://til.hashrocket.com/posts/2dab9b4db4-ruby-array-shortcuts-and-method
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Call a method on every items with &:
So this
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:
Can be reduce to
[:foo, :bar].each(&method(:puts))
Source: https://til.hashrocket.com/posts/2dab9b4db4-ruby-array-shortcuts-and-method
The text was updated successfully, but these errors were encountered: