Hiya, A simple problem I expect, but I cannot seem to find a simple way to do it. How can I check to see if a parameter is a number or not. Bear in mind it may be nil so I need to cope with that situation too. Thanks, ~ Mark -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sorry, I should clarify that I mean a querystring parameter. As in: (in a controller method) params[:some_key] -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
if params[:some_key].to_i != 0 Granted, you''ll need a separate check if the number actually can be 0, but String#to_i returns 0 when there is no number. Jason On 7/9/07, Mark Dodwell <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Sorry, I should clarify that I mean a querystring parameter. As in: > > (in a controller method) > > params[:some_key] > > > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Mark, Mark Dodwell wrote:> How can I check to see if a parameter is a number or > not. Bear in mind it may be nil so I need to cope with > that situation too.In your model... validates_numericality_of :some_key Check the doc at api.rubyonrails.org for a number of options. hth, Bill --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jason Roelofs wrote:> if params[:some_key].to_i != 0 > > Granted, you''ll need a separate check if the number actually can be 0, but > String#to_i returns 0 when there is no number.Depending on the OPs needs he may want to use the method Integer and rescue the ArgumentError - or just use Bills suggestion.> irb(main):001:0> "abc123".to_i > => 0 > irb(main):002:0> "123abc".to_i > => 123 > irb(main):003:0> Integer("abc123") > ArgumentError: invalid value for Integer: "abc123" > from (irb):3:in `Integer'' > from (irb):3 > from :0 > irb(main):004:0> Integer("123abc") > ArgumentError: invalid value for Integer: "123abc" > from (irb):4:in `Integer'' > from (irb):4 > from :0--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Stefan Mahlitz wrote:> Jason Roelofs wrote: >> if params[:some_key].to_i != 0 >> >> Granted, you''ll need a separate check if the number actually can be 0, but >> String#to_i returns 0 when there is no number. > > Depending on the OPs needs he may want to use the method Integer and > rescue the ArgumentError - or just use Bills suggestion.you can also try and match via regexp params[:some_key] =~ /^\d+$/ -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---