rubymetaprogramming

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


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

Solution

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

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