Main Content

Multiple inputs, multiple out, ruby functions

Archive - Originally posted on "The Horse's Mouth" - 2011-04-19 22:49:39 - Graham Ellis

Has it ever struck you as curious that in many languages, you can pass as many parameters into a function as you like, but you can only return one? Well - that's many language not all - in Ruby you can return a whole series of values in a comma separated list, and place each into its own variable.

You can also pass in as manay parameters as you like ... simply specify a * in front of the last parameter in the function definition, and it will be set up as an array that sponges up as many values as necessary. Source code example - [here].

(There's an alternative way of returning multiple values - via an array or hash - [here])

Image - public domain, catalogued by wpclipart