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.
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).
On 4/13/06, Patrick Joyce <patrick.t.joyce@gmail.com> 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.
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.
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:
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:
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
···
2006/4/14, Patrick Joyce <patrick.t.joyce@gmail.com>:
Integer("08") or Integer("038") bomb
"08".to_i + "038".to_i => 46
Cheers
Robert
···
On 4/14/06, Robert Klemme <shortcutter@googlemail.com> wrote:
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.
Integer("oh eight") + Integer("038".sub(/^0+/, ''))
ArgumentError: invalid value for Integer: "oh eight"
from (irb):4:in `Integer'
from (irb):4
···
On Fri, 2006-04-14 at 22:54 +0900, Robert Dober wrote:
On 4/14/06, Robert Klemme <shortcutter@googlemail.com> wrote:
>
> 2006/4/14, Patrick Joyce <patrick.t.joyce@gmail.com>:
> > 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+/, ''
>
> Ruby can do better
Integer("08") or Integer("038") bomb
"08".to_i + "038".to_i => 46