Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 387
Mo. Avatar asked Jul 19 '10 15:07

Mo.


People also ask

How do you hash an array in Ruby?

In Ruby, a hash is a collection of key-value pairs. A hash is denoted by a set of curly braces ( {} ) which contains key-value pairs separated by commas. Each value is assigned to a key using a hash rocket ( => ). Calling the hash followed by a key name within brackets grabs the value associated with that key.

How can you test if an item is included in an array Ruby?

This is another way to do this: use the Array#index method. It returns the index of the first occurrence of the element in the array. This returns the index of the first word in the array that contains the letter 'o'. index still iterates over the array, it just returns the value of the element.


1 Answers

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

like image 67
sepp2k Avatar answered Sep 29 '22 21:09

sepp2k