Ruby:如何将多个方法调用与“发送"链接在一起 [英] Ruby: How to chain multiple method calls together with "send"

查看:85
本文介绍了Ruby:如何将多个方法调用与“发送"链接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须有一种内置的方式,对吗?

There has to be a built in way of doing this, right?

class Object
  def send_chain(arr)
    o=self
    arr.each{|a| o=o.send(a) }
    return o
  end
end

推荐答案

我刚刚遇到了这个问题,它真的请求注入:

I just ran across this and it really begs for inject:

def send_chain(arr)
  arr.inject(self) {|o, a| o.send(a) }
end

这篇关于Ruby:如何将多个方法调用与“发送"链接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆