Ruby/Method/args

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
м (1 версия: Импорт выборки материалов по Ruby)
 

Текущая версия на 17:55, 13 сентября 2010

set arguments along with variable arguments.

# The trick is that the variable list of arguments (the one that starts with *) always 
# comes at the end of the argument list. 
 
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 )