Skip to content

Instantly share code, notes, and snippets.

@plexus
Created October 14, 2013 20:26
Show Gist options
  • Save plexus/6981661 to your computer and use it in GitHub Desktop.
Save plexus/6981661 to your computer and use it in GitHub Desktop.
class Enumerator
def extract(&blk)
self.each do |obj|
blk.call(*blk.parameters.map(&:last).map {|attr| obj.send(attr)})
end
end
end
Treasure = Struct.new(:coins, :gems)
treasures = [
Treasure.new(100, %w[emerald ruby]),
Treasure.new(70, %w[amethyst], [])
]
treasures.each.extract do |coins|
coins # => 100, 70
end
treasures.each.extract do |gems|
gems # => ["emerald", "ruby"], ["amethyst"]
end
@petervandenabeele
Copy link

Thanks for the confirmation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment