Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between these two method parameter definitions?

I'm starting to learn Ruby. I read that arguments where passed by reference to a method, however I don't understand the difference between these two methods.

def print(text)
   puts text
end

and

def print(*text)
    puts text
end

Using a * means that we are passing a pointer like in C?

like image 463
Ben D Avatar asked Sep 23 '26 10:09

Ben D


1 Answers

The *text is what's called the splat operator in Ruby. It basically means if you pass multiple arguments to the second print they will get slurped into the single text variable.

See The Splat Operator in Ruby

like image 149
dj2 Avatar answered Sep 26 '26 03:09

dj2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!