Ruby Basics – * method arguments

If you have ever asked yourself what is behind arguments like *this then here is the trivial answer:

A small ruby script like this:

#!/usr/bin/env ruby

def my_method(a,b,*c)
puts "a: " + a
puts "b: " + b
puts "c:"
c.each { |ce| puts ce }
end

my_method("a1", "b2", "c3", "c4", "c5", "c6")

Produces the following output:

a: a1
b: b2
c:
c3
c4
c5
c6

As you can see all arguments after a and b are collected in an array and passed to c.

Isn’t this nice?

Kommentarfunktion ist deaktiviert