You'll need to run to_sym
on the result of v["foo"]
in order to the the value in k
:
1.9.3p484 :007 > v = { "foo"=>"bar"}
=> {"foo"=>"bar"}
1.9.3p484 :008 > k = {:bar => 1}
=> {:bar=>1}
1.9.3p484 :009 > k[v["foo"].to_sym]
=> 1
Pergunta
v = { "foo"=>"bar"}
v["foo"] // bar
Say,
k = {:bar => 1}
>k[:bar] // 1
But,
k[v["foo"]] // nil
how to access key ==> value ( k[ key ] = value )
from hashes using a variable, say v["foo"]
Solução
You'll need to run to_sym
on the result of v["foo"]
in order to the the value in k
:
1.9.3p484 :007 > v = { "foo"=>"bar"}
=> {"foo"=>"bar"}
1.9.3p484 :008 > k = {:bar => 1}
=> {:bar=>1}
1.9.3p484 :009 > k[v["foo"].to_sym]
=> 1