开发者

returning an array that contains an array and hash in ruby method?

开发者 https://www.devze.com 2023-01-08 10:34 出处:网络
HI is it possible to return an array that contains an array and开发者_如何学编程 hash from a method in ruby?

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


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


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号