views:

43

answers:

3

HI

is it possible to return an array that contains an array and hash from a method in ruby?

i.e

def something
array_new = [another_thing, another_thing_2]
hash_map = get_hash()

return [array_new, hash_map]

end

and to retrieve the array:

some_array, some_hash = something()

thanks

+2  A: 

You will only ever be able to return one thing. What you are returning there is an array containing an array and a hash.

Maletor
yes i know, i phrased the question wrong, sorry. so would i be able to return the array containing an array and hash map?
Mo
Yes you would. Type irb on the command line for an awesome little scratchpad to try stuff out. Just keep a browser open to the API as well ;)
Maletor
+3  A: 

Sure, that's perfectly possible and works exactly as in your example.

sepp2k
+1  A: 

Ruby methods can be treated as if they return multiple values so you can collect the items in an array or return them as separate objects.

def something
  array_new = Array.new
  hash_new = Hash.new
  return array_new, hash_new
end

a, b = something
a.class # Array
b.class # Hash

c = something
c.class # Array
Steve Weet