Confused about the million ways to reach inside Ruby Hash with the symbol sign -


i drowning in million ways access hash.

condition1 = {     "e" =>1 }  condition2 = {     "e":1 }  condition3 = {     :"e" =>3 }   condition4 = {     e:4 }   condition5 = {  # 100% sure not working because of syntax error     e=>5 }  condition6 = {  # 100% sure not working because of syntax error     :e:6 }    condition7 = {     :e=>7 }  puts condition1["e"] puts condition2["e"] puts condition3[:e] puts condition4[:e] puts condition7[:e] 

output:

1                                      (nothing) 3 4 7 

first question: first 2 puts statement, why working first 1 not second one? in both hashes, using strings keys. because used "=>" in first hash , string being read symbols? being processed :"e" =>1?

second question: leads second question third condition, told if used symbol sign before quote, happens in background quote ignored. that's why able access through :e without quotes. true? if that's true, first question becomes more confusing. if in first condition, "e"=>1 evaluated :"e" =>1, shouldn't quotes ignored? it's read :e =>1. how come able access quotes?

third question: how access second condition? know it's not recommended use strings key values. if in case have created strings, how access it? have tried accessing through variable name it's still not outputing anything.

thank reading this. know it's not worth care these details. ran today , can't move on without figuring out heck going on !!

rule of thumb

if there's colon (:) it's symbol. if there's hashrocket (=>), it's whatever left of hashrocket (which can anything).

declaring keys in hash literal

when "hash literal" mean code declares hash curly braces ({ foo: 1 }) or method arguments (bar(baz: 2)).

there 2 ways declare key in hash literal.

keys hashrockets (=>)

the first way declare key hashrocket (=>). when use hashrocket, key whatever value left of it, , can put any kind of object (or expression) left of it:

hashrocket_hash = {   "i string" => 1,   :i_am_a_symbol => 2,   :"i symbol" => 4,   /i regexp!/ => 5,   kernel => 6,   if true "i string" end => 7,   nil => 8 }  p hashrocket_hash.keys # => [ "i string", #      :i_am_a_symbol, #      :"i symbol", #      /i regexp!/, #      kernel, #      "i string", #      nil #    ]  p hashrocket_keys.map(&:class) # => [ string, #      symbol, #      symbol, #      regexp, #      module, #      string, #      nilclass #    ] 

keys colons (:)

the other way declare key colon (:). when use colon resulting key always symbol. usual symbol rules apply (read thorough answer here: what can ruby symbol (syntax) contain?) except colon goes @ end instead of beginning:

colon_hash = {   i_am_a_symbol: 9,   "i symbol": 10 }  p colon_hash.keys # => [ :i_am_a_symbol, #      :"i symbol" ]  p colon_hash.keys.map(&:class) # => [ symbol, #      symbol ] 

accessing hash value

there no special rules accessing hash value. if want access value key symbol, must use symbol. if want access value key string, must use string. if key else, must use thing.

in below examples, pay close attention keys followed colons , followed hashrockets:

hsh1 = { foo: 1 } p hsh1[:foo]   # => 1 p hsh1[:"foo"] # => 1 p hsh1["foo"]  # => nil  hsh2 = { "bar": 2 } p hsh2[:bar]   # => 2 p hsh2[:"bar"] # => 2 p hsh2["bar"]  # => nil  hsh3 = {   kernel: 3,   kernel => 4 } p hsh3[:kernel]  # => 3 p hsh3[kernel]   # => 4 p hsh3["kernel"] # => nil 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -