Bill Kelly
4/17/2007 1:21:00 AM
From: "mwmarkland@yahoo.com" <mwmarkland@gmail.com>
>
> I have been reading _The Ruby Way_ and am confused by the temperature
> conversion sample program on page 14.
>
> The line in question is
>
> abort "#{temp} is not a valid number." if temp !~ /-?\d+/
>
> What I do not understand is that if \d matches "digits", which I
> understand to be [0-9], how can this expression match "98.6", which it
> seems to do just fine.
Hi,
Since that regexp isn't anchored, it only cares whether a digit
appears anywhere in the string.
It would be happy with "abc1def".
If you anchor it, like: /\A-?\d+\z/
Then it will require an exact match, caring about all characters
between the beginning and end of the string.
Hope this helps,
Bill