Created
October 14, 2013 20:26
-
-
Save plexus/6981661 to your computer and use it in GitHub Desktop.
Revisions
-
plexus created this gist
Oct 14, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ 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