From: "Michael W. Ryder" <_mwryder@...> Date: 2008-05-21T11:20:11+09:00 Subject: Re: Is there any good source for the logic behind some of the methods? Phlip wrote: >> I am trying to figure out why methods that seem to do the same thing behave >> so differently. For example Integer("0377") returns 255 as > > Where are you getting these Integer() methods? I never saw them before. (4 > years, aggregate, working in Ruby.) > In Pickaxe they are listed in the Kernel module. I notice while reading it again that the Integer method honors 0x, 0b, etc. when converting strings while Float just uses to_f. Not sure why and this was part of my question. >> expected for an octal number but "0377".to_i returns 377. Or why >> Integer("12e3") crashes while Float("12e3") returns 12000.0 as expected. I >> was hoping to find somewhere that tells when to use one or the other >> method depending upon expected inputs. It is very time consuming to have >> to keep testing a method to find out it's limitations. > > Write unit tests for everything you do, starting with tests that probe the > libraries. This should be second nature; you should write a test for any > question you can think to ask about a system. > That's what I have been doing and this is why I keep having problems. Float(x) works fine until you try Float("0x11"). Then I had to upgrade to Float(Float(x) rescue Integer(x) rescue 0). It would be nice to be able to expect some of these problem cases and program around them, using testing to make sure that the code was correct, not that I used the correct method for the inputs. > And .to_i is for brute-force parsing; not for anything elaborate. Like the > hallowed C atoi(). Yes, I found that out early on in my testing. > >