有没有更简洁的方法在Ruby中调用外部方法?

时间:2013-09-15 19:34:54

标签: ruby enumerable proc

有更简洁的方法吗?

# 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? )

1 个答案:

答案 0 :(得分:4)

是的,可能这样,但对性能不利(见帖子: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.
相关问题