On Fri, Dec 28, 2012 at 5:41 AM, Dami=E1n M. Gonz=E1lez
<lists / ruby-forum.com> wrote:
>> In these cases, I just double convert i.e.
>>
>> irb(main):016:0> buffer =3D 'fred'
>> =3D> "fred"
>> irb(main):017:0> buffer.to_i.to_s =3D=3D buffer
>> =3D> false
>
>  Have to say that that's very cleaver. Very good solution.

Sorry to disagree.  There are more clever solutions:

Use an Integer and deal with the exception.

1. immediately

irb(main):001:0> buffer =3D 'fred'
=3D> "fred"
irb(main):002:0> i =3D Integer(buffer)
ArgumentError: invalid value for Integer(): "fred"
        from (irb):2:in `Integer'
        from (irb):2
        from /usr/bin/irb:12:in `<main>'
irb(main):003:0> begin; i =3D Integer(buffer); puts "int"; rescue
ArgumentError; puts "no int"; end
no int
=3D> nil

2. by converting the value to nil if it is not a number

irb(main):004:0> j =3D Integer(buffer) rescue nil
=3D> nil
irb(main):005:0> if j; puts "int"; else puts "no int"; end
no int
=3D> nil

Kind regards

robert

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