Skip to content

Instantly share code, notes, and snippets.

@edivandecastro
Created December 17, 2015 19:10
Show Gist options
  • Save edivandecastro/f847217799f62c67dd24 to your computer and use it in GitHub Desktop.
Save edivandecastro/f847217799f62c67dd24 to your computer and use it in GitHub Desktop.
Little Things: Introspecting Block Parameters
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