method
[]
![Moderate documentation Importance_2](https://d2vfyqvduarcvs.cloudfront.net/images/importance_2.png?1349367920)
[](*args)
public
Creates a new hash populated with the given objects. Equivalent to the literal { key => value, … }. In the first form, keys and values occur in pairs, so there must be an even number of arguments. The second and third form take a single argument which is either an array of key-value pairs or an object convertible to a hash.
Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200} Hash[ [ ["a", 100], ["b", 200] ] ] #=> {"a"=>100, "b"=>200} Hash["a" => 100, "b" => 200] #=> {"a"=>100, "b"=>200}
Register or
log in
to add new notes.
UnfalseIdeas -
July 19, 2012
pascal -
April 17, 2013
![Default_avatar_30](https://www.gravatar.com/avatar/39e784d62dbda680c5c2ac6d9b9738aa?size=30&default=http://apidock.com/images/default_avatar_30.png)
0 thanks
Assignment using 'key: value'
Another shorthand way of assigning key, value pairs:
Hash[one: 1, two: 2] #=> {:one=>1, :two=>2}
![Default_avatar_30](https://www.gravatar.com/avatar/787520d2eb3ef81e720c06f153765b2a?size=30&default=http://apidock.com/images/default_avatar_30.png)
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?