How do I pass multiple arguments to a ruby method as an array? How do I pass multiple arguments to a ruby method as an array? arrays arrays

How do I pass multiple arguments to a ruby method as an array?


Ruby handles multiple arguments well.

Here is a pretty good example.

def table_for(collection, *args)  p collection: collection, args: argsendtable_for("one")#=> {:collection=>"one", :args=>[]}table_for("one", "two")#=> {:collection=>"one", :args=>["two"]}table_for "one", "two", "three"#=> {:collection=>"one", :args=>["two", "three"]}table_for("one", "two", "three")#=> {:collection=>"one", :args=>["two", "three"]}table_for("one", ["two", "three"])#=> {:collection=>"one", :args=>[["two", "three"]]}

(Output cut and pasted from irb)


Just call it this way:

table_for(@things, *args)

The splat (*) operator will do the job, without having to modify the method.


class Hello  $i=0  def read(*test)    $tmp=test.length    $tmp=$tmp-1    while($i<=$tmp)      puts "welcome #{test[$i]}"      $i=$i+1    end  endendp Hello.new.read('johny','vasu','shukkoor')# => welcome johny# => welcome vasu# => welcome shukkoor