0

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!

1 Answer 1

2

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.