Forum: Ruby Strange Integer() behavior

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
27d51f62ee0d0042cd2f506cf46981b4?d=identicon&s=25 Patrick Joyce (patrickjoyce)
on 2006-04-14 08:36
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.
C1bcb559f87f356698cfad9f6d630235?d=identicon&s=25 Hal Fulton (Guest)
on 2006-04-14 08:43
(Received via mailing list)
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
6fdf688ae3723b69d07c9de4bb3378e1?d=identicon&s=25 J Irving (Guest)
on 2006-04-14 08:46
(Received via mailing list)
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
27d51f62ee0d0042cd2f506cf46981b4?d=identicon&s=25 Patrick Joyce (patrickjoyce)
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-04-14 11:45
(Received via mailing list)
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
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2006-04-14 15:54
(Received via mailing list)
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
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-04-14 16:31
(Received via mailing list)
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
This topic is locked and can not be replied to.