reuben.m-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2010-Feb-05 16:44 UTC
How to use a variable as a params hash key
Not sure if I worded that Subject line correctly... :P 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 5, 2010, at 8:44 AM, reuben.m-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Not sure if I worded that Subject line correctly... :P > > 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?You probably want (note the .to_s) if params[:data][number.to_s] == ''1'' -philip -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 5, 4:44 pm, "reube...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <reube...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Not sure if I worded that Subject line correctly... :P > > 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> 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
reuben.m-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2010-Feb-05 18:18 UTC
Re: How to use a variable as a params hash key
I had tried that as well, but it didn''''t make any difference. On Feb 5, 11:13 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 5, 4:44 pm, "reube...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <reube...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Not sure if I worded that Subject line correctly... :P > > > 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 > > > 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 5, 6:18 pm, "reube...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <reube...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I had tried that as well, but it didn''''t make any difference.what did you try ? Fred> > On Feb 5, 11:13 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > On Feb 5, 4:44 pm, "reube...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <reube...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Not sure if I worded that Subject line correctly... :P > > > > 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 > > > > 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
reuben.m-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2010-Feb-05 18:27 UTC
Re: How to use a variable as a params hash key
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 5, 6:18 pm, "reube...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <reube...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I had tried that as well, but it didn''''t make any difference. > > what did you try ? > > Fred > > > > > On Feb 5, 11:13 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > > On Feb 5, 4:44 pm, "reube...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <reube...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Not sure if I worded that Subject line correctly... :P > > > > > 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 > > > > > 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.