0

I wrote this simple program in Ruby

h = {}
h["a"] = "1"
h[:a] = "2"

puts h
h.each { |k, v| puts "#{k} => #{v}" }

Which outputs

{"a"=>"1", :a=>"2"}
a => 1
a => 2

I have a few questions about this code which involve : and " as indexers not by themselves

  1. When would I use one over the other? I originally found out there was a difference when certain mongo/rails functions I was calling were depended on the : indexer, while when I parsed json only " indexers were present.
  2. Are there other ways to index a value with a key?
  3. Is there a way for my each to recognize the difference between : and " indexers? It seems that puts was able to determine but each was not
Logan Murphy
  • 6,120
  • 3
  • 24
  • 42

1 Answers1

0

{} creates and empty Hash

:key is a symbol and 'key' is string

Please first of all refer this about Hash

Hetal Khunti
  • 787
  • 1
  • 9
  • 23