【问题标题】:Is it possible to backport "yield_self" to Ruby versions prior to 2.5?是否可以将“yield_self”反向移植到 2.5 之前的 Ruby 版本?
【发布时间】:2022-07-07 19:49:02
【问题描述】:

我有一个“无法升级”的旧版应用程序,我缺少最新 Ruby 版本中的 yield_self 功能。

有没有办法将其反向移植到 2.5 之前的 Ruby 版本(在其中使用)?

一个例子:

"data.csv"
  .yield_self { |name| File.expand_path(name, __dir__) }
  .yield_self { |path| File.read(path) }
  .yield_self { |body| CSV.parse(body) }
  .map        { |row|  row[1].to_i }
  .sum

【问题讨论】:

    标签: ruby


    【解决方案1】:

    看起来很简单,

    class Object
      def yield_self(*args)
        yield(self, *args)
      end
    end
    

    【讨论】:

    • 为什么是*argsyield_self 不接受任何参数,是吗?
    猜你喜欢
    • 1970-01-01
    • 2011-06-15
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    相关资源
    最近更新 更多