Reputation: 55
I'm struggling with an issue in getting value from array with variable as an index.
I'm trying to get value from array "ok"
ok = {"3"=>"on", "4"=>"on", "6"=>"on", "7"=>"on"}
In my code (in my model):
@i=0;
@veh = VehicleClass.order(id: :asc)
@veh.each do |veh|
@i = veh.id;
checkbox = ok[@i];
end
Interesting is that, when I try to call the array by:
checkbox = ok['3'] => I got the value "on"
in case of
@i=3; checkbox = ok[@i] => result NULL.
I was trying many options like:
checkbox = ok[@i]
checkbox = ok['@i']
checkbox = ok['#{@i}']
checkbox = ok['#{i}']
checkbox = ok[i]
checkbox = ok[:i]
Nothing works..:(
Any thoughts / suggestions?
Thanks!
Upvotes: 0
Views: 22
Reputation: 23661
Use double quotes for interpolation
checkbox = ok["#{@i}"]
You are using single quotes that's why it's not working
or you can also use to_s
checkbox = ok[@i.to_s]
Upvotes: 2