Created
October 14, 2013 20:26
-
-
Save plexus/6981661 to your computer and use it in GitHub Desktop.
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 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 |
Thanks for the confirmation.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you're right, that empty array is a leftover from earlier code.