arr = [{a:1}, {a:2}],要得到 [1,2],一般这样写:
arr.map{|hash| hash[:a]}
我知道有一种写法是:
['a','b'].map(&:upcase)
如果能写成这样就好了: arr.map(&:[:a])
当然,这样是报错的。
有没有类似的写法呢?
ruby 内置的方法应该是没有,map 不支持额外的参数,所以也没有 arr.map(&:[], :a)。自己搞一个:
class Array
def map_hash_value(key)
map { |x| x[key] }
end
end
[{a: 1}, {a: 2}].map_hash_value(:a)
# => [1, 2]
extract = ->(k) { ->(h) { h[k] } }
arr.map(&extract[:a])
你可以给 Symbol 加个 patch
class Symbol
def call(*args, &block)
->(caller, *rest) { caller.send(self, *rest, *args, &block) }
end
end
这样任意的 proc shorthand 写法就可以写成
arr.map(&:[].(:a))
但是这样的可读性反而比不上非 shorthand 写法,记得 ruby 有讨论一个新的语法支持 shorthand argument, 语法类似这样,&0, &1, &N 代表 proc 的第 N 个元素,不过还没有被接受:
arr.map(&0[:a])