On Fri, Mar 29, 2013 at 5:38 PM, Joel Pearson <lists / ruby-forum.com> wrote:

> Sometimes it's best to let the errors occur, there's already a certain
> amount of error-handling in Ruby.
>
> Anyway, this should do what you want:
>
> def Add_two_numbers(x,y)
> [x,y].each { |num|
> fail ArgumentError, "#{num} is not a number" unless num.is_a? Fixnum
> }
> x + y
> end
>

Better use Integer for the test because Fixnums turn to Bignums from a
certain number on.  Using #to_int or Integer() is a simpler approach as it
includes the exception throwing code with a meaningful error message
already:

irb(main):023:0> Integer("x")
ArgumentError: invalid value for Integer(): "x"
        from (irb):23:in `Integer'
        from (irb):23
        from /usr/bin/irb:12:in `<main>'
irb(main):024:0> "x".to_int
NoMethodError: undefined method `to_int' for "x":String
        from (irb):24
        from /usr/bin/irb:12:in `<main>'


Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/