Technical
Validate Numericality Of

What if you CANNOT use validates_presence_of, in particular if your IT havy on store procidures?

This stub ( paritally plucked out of Rails Api ) lets you to validate wheter the parameter passed to the controller ( they always send stringified ) is numeric or not.


#!/usr/bin/env ruby

require 'test/unit'

class TestNumeric < Test::Unit::TestCase

 def test_validate_numericality_of
  def test_numeric?
    assert "3".numeric?
    assert !"a".numeric?
  end
 end  

end

class Object
  def numeric?
    begin
     return true if Kernel.Float(self)
    rescue ArgumentError, TypeError
     return false
    end
  end
end