Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Hash from an array - how does this work?

Tags:

ruby

hash

splat

fruit = ["apple","red","banana","yellow"]
=> ["apple", "red", "banana", "yellow"]

Hash[*fruit]    
=> {"apple"=>"red", "banana"=>"yellow"}

Why does the splat cause the array to be so neatly parsed into the Hash?

Or, more precisely, how does the Hash "know" that "apple" is the key and "red" is its corresponding value?

Is it simply because they are at consecutive positions in the fruit array?

Does it matter that the splat is used here? Can a Hash not define itself from an arry so directly otherwise?

like image 398
uzo Avatar asked Sep 22 '09 04:09

uzo


People also ask

Can you hash an array?

Python Hashing (Hash tables and hashlib) While an array can be used to construct hash tables, array indexes its elements using integers. However, if we want to store data and use keys other than integer, such as 'string', we may want to use dictionary. Dictionaries in Python are implemented using hash tables.

How do you store an array in hash?

To assign that array to a hash element, you'd use either $b{"x"} = [@a] or $b{"x"} = \@a , depending on what you're trying to do. [@a] makes a new arrayref containing a copy of the current contents of @a . If the contents of @a change after that, it has no effect on $b{x} .

Can you hash an array in Java?

hashCode(Object[]) method returns a hash code based on the contents of the specified array. If the array contains other arrays as elements, the hash code is based on their identities rather than their contents. For any two arrays a and b such that Arrays.

Which one is a difference between an array and a hash?

A Hash is a collection of key-value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.


2 Answers

As the documentation states:

Hash["a", 100, "b", 200]       #=> {"a"=>100, "b"=>200}
Hash["a" => 100, "b" => 200]   #=> {"a"=>100, "b"=>200}
{ "a" => 100, "b" => 200 }     #=> {"a"=>100, "b"=>200}

You can't pass an array to the Hash[] method according documentation, thus the splat is just a way to explode the fruit array and pass its elements as normal arguments to the Hash[] method. Indeed this is a very common use of the splat operator.

The cool thing is that if you try to pass an odd number of arguments to Hash you will get an ArgumentError exception:

fruit = ["apple","red","banana","yellow","orange"]
#=> ["apple", "red", "banana", "yellow", "orange"]
Hash[*fruit] #=> ArgumentError: odd number of arguments for Hash
like image 191
khelll Avatar answered Sep 25 '22 20:09

khelll


Look at the public class method [] in class Hash. (Say, over here.) It clearly states that a new Hash (instance) will be created and populated with the given objects. Naturally, they occur in pairs. The splat operator essentially expands an array when used as a parameter.

like image 32
DigitalRoss Avatar answered Sep 23 '22 20:09

DigitalRoss