Mixed parameters and varied length parameter : Variable Number Arguments « Method « Ruby






Mixed parameters and varied length parameter

#!/usr/bin/env ruby

def two_plus( one, two, *args )
  length = args.size
  label = length == 1 ? " variable argument" : " variable arguments"
  num = length.to_s + label + " (" + args.inspect + ")"
  num
end

puts two_plus( 1, 2 )

puts two_plus( 1000, 3.5, 14.3 )

puts two_plus( 100, 2.5, "three", 70, 14.3 )

 








Related examples in the same category

1.Variable Arguments
2.Passing a Variable Number of Arguments
3.prefixing the array with an asterisk
4.A variable number of arguments stored in an array.