Ruby: How to chain multiple method calls together with "send"

Zando picture Zando · Nov 4, 2010 · Viewed 13.8k times · Source

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

Answer

Joe picture Joe · Aug 22, 2012

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

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