Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created November 29, 2011 04:45
Show Gist options
  • Select an option

  • Save pasberth/1403463 to your computer and use it in GitHub Desktop.

Select an option

Save pasberth/1403463 to your computer and use it in GitHub Desktop.
こんなの便利だなあって思った
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