Skip to content

Instantly share code, notes, and snippets.

@plexus
Created October 14, 2013 20:26
Show Gist options
  • Select an option

  • Save plexus/6981661 to your computer and use it in GitHub Desktop.

Select an option

Save plexus/6981661 to your computer and use it in GitHub Desktop.

Revisions

  1. plexus created this gist Oct 14, 2013.
    22 changes: 22 additions & 0 deletions extract.rb
    Original 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