这个问题已经有了答案:
How to turn a Ruby method into a block?
3答
有没有更简洁的方法来做这件事?
# Given a directory containing subdirectories: foo, bar
targets = ['./foo', './bar', './free']
targets.map{ |d| Dir.exists? d }
# => [ true, true, false ]
我希望能做一些类似于proc调用的事情…感觉更干净:
# targets.map( Dir.exists? )
最佳答案
是的,可以这样做,但不利于性能(请参见文章:Is the &method(:method_name) idiom bad for perfomance in Ruby?):
targets = ['./foo', './bar', './free']
targets.map(&Dir.method(:exists?))
# => [false, false, false] #all are false,as I don't have such directories.