How to use a variable as a params hash key

Not sure if I worded that Subject line correctly... :stuck_out_tongue:

Anyway, I want to be able to do something similar to the following
when processing form data:

1..5.each do |number|
  if params[:data][number] == '1' # <-- using block variable
"number" as a hash key does not seem to work...
    foo.bar
  end
end

The if statement is never evaluated as true, but at the same time no
errors pop up.

What is the proper means of doing the equivalent?

-Reuben

You probably want (note the .to_s)

if params[:data][number.to_s] == '1'

-philip

Not sure if I worded that Subject line correctly... :stuck_out_tongue:

Anyway, I want to be able to do something similar to the following
when processing form data:

Your problem here is that the hash keys are strings, not numbers

Fred

I had tried that as well, but it didn''t make any difference.

I had tried that as well, but it didn''t make any difference.

what did you try ?

Fred

Never mind I just figured out what I was doing.

I didn't realized that the block variable I was passing in my code was
itself a hash, so what I needed was:

if params[:data][number[:number].to_s] == '1'

Works now.

Thanks!
-Reuben