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

标签 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? )

最佳答案

是的,可以这样,但不利于性能(参见帖子: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.

关于ruby - 有没有更简洁的方法在 Ruby 中调用 map 上的外部方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52148706/

相关文章:

javascript - Rails + 图表.js : How to fill out the Javascript array with value from database?

ruby-on-rails - 在某些 RSpec rails 请求示例中测试 HTTP 状态代码,但在其他示例中测试引发的异常

java - 如何在 Java 中创建一个空流?

arrays - 为什么在Elixir中无法枚举元组?

linux - linux i686 上的 current_task 是什么?

java - .NET 上没有的 Java/Ruby 等很酷的框架有哪些?

ruby - 线程安全的 Ruby 解释器是什么意思?

ruby - 按降序对可枚举进行排序

sockets - tm->when in/proc/net/tcp 的格式是什么?

list - 如何将列表传递给 TCL 中的 proc?