Are the colons in the second technique part of the syntax or parts of the string? And if they are just parts of the strings, why aren't the strings in quotes?
Looking at the Rubydoc we can see that the Hash::[] method has three forms:
Hash[ key, value, ... ] → new_hash
Hash[ [ [key, value], ... ] ] → new_hash
Hash[ object ] → new_hash
Your specific example is an instance of the first case where a list of objects are interpreted as pairs of key-values. So calling:
Hash[a, b, c, d, e, f]
will pair:
I think you are confused by the : syntax, for which you can easily take a look at some topics here on SO. Just to summarize: the : in this case constructs a Symbol object.
In conclusion:
Hash[:punch, 99, :kick, 98, :stops_bullets_with_hands, true]
can be represented as the following hash:
{
:punch => 99,
:kick => 98,
:stops_bullets_with_hands => true
}
where :punch, :kick and :stops_bullets_with_hands are symbols and 99, 98 and true the corresponding values.
To return the values you can simply use the Hash#[] method like this:
chuck_norris[:punch] # 99
chuck_norris[:kick] # 98
chuck_norris[:stops_bullets_with_hands] # true
Remember that you can convert a symbol to a string via the #to_s method:
:punch.to_s # "punch"
:kick.to_s # "kick"
:stops_bullets_with_hands.to_s # "stops_bullets_with_hands"