MyTetra Share
Делитесь знаниями!
Variable Number of Parameters
Время создания: 31.08.2017 21:03
Текстовые метки: knowledge
Раздел: Ruby
Запись: xintrea/mytetra_db_mcold/master/base/15005420977lvljy2pn0/text.html на raw.githubusercontent.com

Variable Number of Parameters

Suppose you declare a method that takes two parameters, whenever you call this method, you need to pass two parameters along with it.

However, Ruby allows you to declare methods that work with a variable number of parameters. Let us examine a sample of this −

#!/usr/bin/ruby

def sample (*test)
   puts "The number of parameters is #{test.length}"
   for i in 0...test.length
      puts "The parameters are #{test[i]}"
   end
end
sample "Zara", "6", "F"
sample "Mac", "36", "M", "MCA"

In this code, you have declared a method sample that accepts one parameter test. However, this parameter is a variable parameter. This means that this parameter can take in any number of variables. So, the above code will produce the following result −

The number of parameters is 3
The parameters are Zara
The parameters are 6
The parameters are F
The number of parameters is 4
The parameters are Mac
The parameters are 36
The parameters are M
The parameters are MCA
Так же в этом разделе:
 
MyTetra Share v.0.59
Яндекс индекс цитирования