logo
  • Jobs
  • About Me
  • Contact
  • Home
« Investigating RSpactor
Blasting open source »

Ruby strings and is_int?

Posted April 2nd, 2008 by Matt Berther

Lately, I’ve found myself needing to do some parameter checking to ensure that appropriate data makes it back to my models when doing finds. Using the will_paginate plugin allows me to very easily do paging on my model data. I simply have to provide the page to the options hash.

class Model < ActionRecord::Base
  def self.search(parameter, page)
    paginate :per_page => 10, :page => page,
             :conditions => ['model.parameter = ?', parameter]
  end
end

The problem here is that some people are trying to pass in URLs as the page parameter, causing exceptions to be raised. I’ve installed Jamis Buck’s ExceptionNotifier plugin and this is informing me via email of every exception encountered in the application. On a side note, I cannot recommend this plugin enough if you want to see where your application is being used incorrectly. I’ve found numerous edge cases with this plugin and urge every Rails installation to take advantage of it.

I wrote a spec for what I was after that looked like this:

describe Model do
  it "should convert the page number to one if an integer is not passed" do
    Model.should_receive(:paginate).with(hash_including(:page => "1"))
    Model.search("parameter", "some_invalid_page_number")
  end
end

Of course this test failed, so I set out to make it pass. What I really needed in this case was a way to reset the page parameter to an integer value if it was not an integer. I scoured the Ruby and Rails documentation but did not find anything built in to the framework.

Having seemingly no other option but to monkey-patch the string class, I ended up with this:

class String
  def is_int?
    self =~ /^[-+]?[0-9]*$/
  end
end

and the first line of the search method in my model looks like this:

page = "1" if not page.is_int?

My specification passed, but surely there’s a better way. I really do not want to monkey patch classes if I dont need to.

This entry was posted on Wednesday, April 2nd, 2008 at 8:46 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Robin Curry
April 2nd, 2008

to_i will convert a non-numeric string value to 0, so you could do something like:

page = “1″ if page.to_i < 1

Robin Curry
April 2nd, 2008

Ha, here’s another way to do it (can’t take credit for this, I found it somewhere else):

def is_int? self.to_i.to_s == self end

Matt Berther
April 2nd, 2008

@robin: thanks for the tips. I actually prefer the first, since I dont end up monkey-patching the string class unnecessarily. I also found that I need to validate my page parameter for nil as well, so the line looks like this:

page = “1″ if not page.nil? and page.to_i < 1

Rik Hemsley
April 3rd, 2008

Converting a string to an integer is FAQ #1 on IRC.

The usual answer given is:

Integer(s) rescue default

… where default is whatever you want the result to be if s won’t convert to an Integer.

Matt Berther
April 3rd, 2008

@rik: I think I need to spend more time in IRC and some more time with Ruby in general. I think the point of my post was that I was just surprised that this was not available in Ruby already. Your method for doing this is exactly the same as I would do it in my .NET projects.

Fred Bruneteau
April 14th, 2008

The Robin’s tip is interesting because it’s realy test the numeric value.
Be careful of the method to_i( ) of the class String, I have detected this behaviour :

irb > "47azerty".to_i
=> 47
irb >

The method to_i( ) only used the digits at the beginning of the string.

N.B. : I use Ruby 1.8.6

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

Social
  • mattberther on twitter
Syndication
Archives
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
Jobs
mattberther.com © 2003 - 2008