2

Here is how I've been creating hashes thus far (per Codecademy)

my_hash = {
  "pay" => "low",
  "vacation" => "yes",
  "parking" => "no"
}

And here is rubymonk's new convenience method of creating hashes.

chuck_norris = Hash[:punch, 99, :kick, 98, :stops_bullets_with_hands, true]

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?

2
  • It has nothing particularly to do with hashes.
    – sawa
    Jul 28, 2013 at 15:48
  • it is indeed a duplicate. Didn't know they were symbols objects. Going to go read that post now. Thx
    – dwilbank
    Jul 28, 2013 at 15:53

2 Answers 2

2

Are the colons in the second technique part of the syntax or part of the string?

The : are not the part of the Hash creation syntax,You are seeing it there as your second example use symbols as keys. :punch,:kick,:stops_bullets_with_hands are Symbol objects.Hash::[] is the method used to create the Hash object.

Hash[:punch, 99, :kick, 98, :stops_bullets_with_hands, true]
     |_________| |________|
     (key,value) (key,value)

In the second example,the keys are considered as symbols. But you can use string,fixnum etc. too.

3
  • ah - a totally new object type introduced into the lesson with no warning! So they are NOT part of the syntax for creating a hash. I should just look at :punch, :kick, etc as if they are integers or strings. Right?
    – dwilbank
    Jul 28, 2013 at 15:47
  • @Taymon How? Hash::[] method need only (key,value) pairs,it doesn't matters what objects the key,value combinations should be.. Jul 28, 2013 at 15:57
  • This should be actually in the Rubymonk forum. But there is no such forum. The problem was, they introduced symbols (with colons) into a very basic-level lesson about creating hashes using a new technique. The colons looked like part of the new syntax. Thanks for clearing it up sirs!
    – dwilbank
    Jul 28, 2013 at 16:04
0

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:

  • a => b
  • c => d
  • e => f

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"

Not the answer you're looking for? Browse other questions tagged or ask your own question.