Created
September 9, 2013 17:55
-
-
Save diego-aslz/6499149 to your computer and use it in GitHub Desktop.
How does the `&:name` parameter work?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cities = City.all | |
| cities.map(&:name) # ['City 1', 'City 2'] |
Author
Author
Thanks.
Author
It uses the to_proc method, so it's possible to do this:
class Greeter
def to_proc
lambda { "hello world" }
end
end
def greet
yield
end
greet &Greeter.new #=> "hello world"And this:
class String
def to_proc
text = self
lambda { eval text }
end
end
instance_eval &"2 + 2" #=> 4Nice! Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found the answer here: