Created
December 17, 2015 19:10
-
-
Save edivandecastro/f847217799f62c67dd24 to your computer and use it in GitHub Desktop.
Little Things: Introspecting Block Parameters
This file contains 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
class Hash | |
module Using | |
def using(&block) | |
values = block.parameters.map do |type, name| | |
self[name] | |
end | |
block.call(*values) | |
end | |
end | |
include Using | |
end | |
hash = { | |
first_name: "John", | |
last_name: "Smith", | |
age: 35 | |
} | |
hash.using do |first_name, last_name, age| | |
puts "Hello, #{first_name} #{last_name}. #{age}" | |
end | |
circle = { | |
radius: 5, | |
color: "blue" | |
} | |
area = circle.using { |radius| Math::PI * radius**2 } | |
puts area |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment