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

On Feb 5, 2010, at 8:44 AM, [email protected] wrote:

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?

You probably want (note the .to_s)

if params[:data][number.to_s] == ‘1’

-philip

On Feb 5, 4:44 pm, “[email protected][email protected] wrote:

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.

On Feb 5, 11:13 am, Frederick C. [email protected]

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

On Feb 5, 12:20 pm, Frederick C. [email protected]

On Feb 5, 6:18 pm, “[email protected][email protected] wrote:

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

what did you try ?

Fred