Created
November 29, 2011 04:45
-
-
Save pasberth/1403463 to your computer and use it in GitHub Desktop.
こんなの便利だなあって思った
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 characters
| module Enumerable | |
| def each_of key, &blk | |
| return Enumerator.new self, :each_of, key unless block_given? | |
| each do |e| | |
| blk.call e[key] | |
| end | |
| end | |
| end | |
| res = [[1, 2], [3, 4]].each_of(0).map do |i| | |
| puts i | |
| i | |
| end | |
| p res | |
| # 1 | |
| # 3 | |
| # [1, 3] | |
| res = [{a: 1, b: 2}, {a: 3, c: 4}].each_of(:a).map do |i| | |
| puts i | |
| i | |
| end | |
| p res | |
| # 1 | |
| # 3 | |
| # [1, 3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment