I am learning Ruby and have encountered an error on a call to
'Integer("039")' in
gems/ruby-web-1.1.1/lib/web/htmlparser/sgml-parser.rb while working on a
little screen scraper project.
Could someone please explain why the first three examples below succeed,
but the fourth fails?
patrick-joyces-computer:~/projects patrick$ ruby -e 'Integer("07")'
patrick-joyces-computer:~/projects patrick$ ruby -e 'Integer("7")'
patrick-joyces-computer:~/projects patrick$ ruby -e 'Integer("8")'
patrick-joyces-computer:~/projects patrick$ ruby -e 'Integer("08")'
-e:1:in `Integer': invalid value for Integer: "08" (ArgumentError)
from -e:1
It seems that "01" - "07" and any numbers without a leading zero work,
but any number greater than 7 with a leading zero throws an
ArgumentError.
Any advice would be greatly appreciated, as my program is failing on a
certain page I am trying to scrape within the ruby-web gem and I am no
where near confident enough to start messing around with those
libraries.
On the off chance that this is a versioning issue I am running Ruby
1.8.4 on OSX 10.4.6.
Thank you in advance for your help.
on 2006-04-14 08:36
on 2006-04-14 08:43
Patrick Joyce wrote: > I am learning Ruby and have encountered an error on a call to > 'Integer("039")' in > gems/ruby-web-1.1.1/lib/web/htmlparser/sgml-parser.rb while working on a > little screen scraper project. The leading zero leads Ruby to think it's octal, but 9 isn't valid in an octal number. I think "039".to_i would work (as it doesn't assume octal). Hal
on 2006-04-14 08:46
Hey Patrick On 4/13/06, Patrick Joyce <patrick.t.joyce@gmail.com> wrote: > patrick-joyces-computer:~/projects patrick$ ruby -e 'Integer("8")' > patrick-joyces-computer:~/projects patrick$ ruby -e 'Integer("08")' > -e:1:in `Integer': invalid value for Integer: "08" (ArgumentError) > from -e:1 > > It seems that "01" - "07" and any numbers without a leading zero work, > but any number greater than 7 with a leading zero throws an > ArgumentError. That's because the leading 0 denotes base 8 (octal). You might want to do this stuff using irb - it helps clarify stuff like this: irb(main):011:0> Integer("077") => 63 cheers J
on 2006-04-14 09:23
Thank you both for the quick answer. In hindsight, the issue is so obvious, I'm almost sorry for wasting your time. Guess the brain was fried after 9-5 coding at the day job and 8PM - 3 AM on my own thing. I guess the project for tomorow is to figure out where that "039" is coming from in the Mechanize parser. Thanks again for the quick response I don't think I would have been able to go to sleep otherwise. J Irving wrote: > Hey Patrick > > On 4/13/06, Patrick Joyce <patrick.t.joyce@gmail.com> wrote: >> patrick-joyces-computer:~/projects patrick$ ruby -e 'Integer("8")' >> patrick-joyces-computer:~/projects patrick$ ruby -e 'Integer("08")' >> -e:1:in `Integer': invalid value for Integer: "08" (ArgumentError) >> from -e:1 >> >> It seems that "01" - "07" and any numbers without a leading zero work, >> but any number greater than 7 with a leading zero throws an >> ArgumentError. > > That's because the leading 0 denotes base 8 (octal). > > You might want to do this stuff using irb - it helps clarify stuff like > this: > > irb(main):011:0> Integer("077") > => 63 > > cheers > J
on 2006-04-14 11:45
2006/4/14, Patrick Joyce <patrick.t.joyce@gmail.com>: > Thank you both for the quick answer. > > In hindsight, the issue is so obvious, I'm almost sorry for wasting your > time. Guess the brain was fried after 9-5 coding at the day job and 8PM > - 3 AM on my own thing. > > I guess the project for tomorow is to figure out where that "039" is > coming from in the Mechanize parser. Well, if you want to use Integer then the obvious fix is to do str.sub /^0+/, '' > Thanks again for the quick response I don't think I would have been able > to go to sleep otherwise. :-) Cheers robert
on 2006-04-14 15:54
On 4/14/06, Robert Klemme <shortcutter@googlemail.com> wrote: > > > -- > Have a look: http://www.flickr.com/photos/fussel-foto/ > > Ruby can do better Integer("08") or Integer("038") bomb "08".to_i + "038".to_i => 46 Cheers Robert -- Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein
on 2006-04-14 16:31
On Fri, 2006-04-14 at 22:54 +0900, Robert Dober wrote: > "08".to_i + "038".to_i => 46 > It's often a bit too relaxed, though: "oh eight".to_i + "038".to_i # => 38 Whereas: Integer("oh eight") + Integer("038".sub(/^0+/, '')) ArgumentError: invalid value for Integer: "oh eight" from (irb):4:in `Integer' from (irb):4
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.