5

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

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

yes i know, i phrased the question wrong, sorry. so would i be able to return the array containing an array and hash map?
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 ;)
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.