Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save evaldasg/bda42153660f9921f18d to your computer and use it in GitHub Desktop.
Save evaldasg/bda42153660f9921f18d to your computer and use it in GitHub Desktop.
Hash#using
class Hash
def using(&block)
values = block.parameters.map do |(type, name)|
self[name]
end
block.call(*values)
end
end
hash = {
first_name: "John",
last_name: "Smith",
age: 35,
# ...
}
hash.using do |first_name, last_name|
puts "Hello, #{first_name} #{last_name}."
end
circle = {
radius: 5,
color: "blue",
# ...
}
area = circle.using { |radius| Math::PI * radius**2 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment