[](...) public

Creates a new hash populated with the given objects. Equivalent to the literal { key, value, … }. Keys and values occur in pairs, so there must be an even number of arguments.

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}
Show source
Register or log in to add new notes.
July 19, 2012
0 thanks

Assignment using 'key: value'

Another shorthand way of assigning key, value pairs:

Hash[one: 1, two: 2] #=> {:one=>1, :two=>2}
April 17, 2013
0 thanks

@UnfalseIdeas

What is the purpose of

Hash[one: 1, two: 1]

When you can write

{one: 1, two: 2}

Aren’t you just passing a hash into the [] method?