Supply any number of arguments (or none).

By 11/09/2019 Arguments, Ruby

To do this, put a star (an asterisk, *) in front of a single argument name:

def obj.multi_args(*x)
  puts "I can take zero or more arguments!"
end

Example 2:

def mixed_args(a,b,*c,d)
  puts "Arguments:"
  p a,b,c,d
end 

mixed_args(1,2,3,4,5)
Arguments:
1
2
[3, 4]
5

If not contain extra arguments:

mixed_args(1,2,3)

Result:

1

2

[]

3

 

codeByPasha

Author codeByPasha

More posts by codeByPasha

Leave a Reply