A variable number of arguments stored in an array. : Variable Number Arguments « Method « Ruby






A variable number of arguments stored in an array.


def num_args( *args )
  length = args.size
  label = length == 1 ? " argument" : " arguments"
  num = length.to_s + label + " ( " + args.inspect + " )"
  num
end

puts num_args

puts num_args(1)

puts num_args( 100, 2.5, "three" )

 








Related examples in the same category

1.Variable Arguments
2.Passing a Variable Number of Arguments
3.prefixing the array with an asterisk
4.Mixed parameters and varied length parameter