title: Ruby programmatically calling method, with variable number of args
body: <p>I am trying to do something similar to this: </p>
  
  <pre><code>def foo(mode= :serial)
  
    if (mode == :serial) then
      self.send(:bar, "one_type")
    else
      self.send(:bar,"second_type",:T5)
    end
  end
  </code></pre>
  
  <p>I can obviously type this out like this. </p>
  
  <p>But I've recently tried expanding it to include a second function like this:</p>
  
  <pre><code>def foo(mode= :serial)
  
    if (mode == :serial) then
      self.send(:bar, "one_type")
      self.send(:foobar, "one_type",20)
    else
      self.send(:bar,"second_type",:T5)
      self.send(:foobar, "one_type",20,:T5)
    end
  end
  </code></pre>
  
  <p>I can still continue as it is, but I thought to myself, there's a pattern here, I should abstract the arguments away into another layer and make this simpler. </p>
  
  <p>So what I wanted to do was something like this: </p>
  
  <pre><code>arg_arr   = [:bar, "one_type"]
  arg_arr_2 = [:foobar, "one_type", 20]
  if (mode == :serial) then
    self.send(arg_arr)
    self.send(arg_arr_2)
  else
    arg_arr &lt;&lt; :T5
    arg_arr2 &lt;&lt; :T5
    self.send(arg_arr)
    self.send(arg_arr2 )
  end
  </code></pre>
  
  <p>I tried some other ideas involving .each, .inspect, but nothing that would work (the usual error was can't convert array into string, which I'm guessing refers to the fact that it treats the array as the entire function name). I can do it if I explicitly say "use array elements[0] , [1] etc, but that just seems wasteful. </p>
  
  <p>Is there a way to achieve this without writing code that is hardcoded to the number of arguments? </p>