Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions




Ruby Programming
Previous Page Home Next Page

Expanding Arrays in Method Calls

Earlier we saw that if you put an asterisk in front of a formal parameter in a method definition, multiple arguments in the call to the method will be bundled up into an array. Well, the same thing works in reverse.

When you call a method, you can explode an array, so that each of its members is taken as a separate parameter. Do this by prefixing the array argument (which must follow all the regular arguments) with an asterisk.

def five(a, b, c, d, e)
  "I was passed #{a} #{b} #{c} #{d} #{e}"
five(1, 2, 3, 4, 5 ) "I was passed 1 2 3 4 5"
five(1, 2, 3, *['a', 'b']) "I was passed 1 2 3 a b"
five(*(10..14).to_a) "I was passed 10 11 12 13 14"

Ruby Programming
Previous Page Home Next Page

  Published under the terms of the Open Publication License Design by Interspire