Forum: Ruby Inconsistent Behavior Converting String to Integer/Float

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.
Daniel H. (Guest)
on 2006-02-25 17:07
$ruby-yarv -v
ruby 2.0.0 (Base: Ruby 1.9.0 2006-02-14) [powerpc-darwin8.5.0]
YARVCore 0.4.0 Rev: 466 (2006-02-22) [opts: ]

$ruby -v
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.3.0]

# Correct behavior
num = "+3"
Float(num)   # -> 3.0
Integer(num) # -> 3

# Strange behavior
num = "3e2"
Float(num)   # -> 300.0
Integer(num) # -> ArgumentError

Why is an ArgumentError raised in the second example?

OT: ruby-talk seems to be rejecting my email. I sent this twice, and
both times it did not make it to the list.

-- Daniel
ts (Guest)
on 2006-02-25 17:17
(Received via mailing list)
>>>>> "D" == Daniel H. <removed_email_address@domain.invalid> writes:

D> $ruby-yarv -v
D> ruby 2.0.0 (Base: Ruby 1.9.0 2006-02-14) [powerpc-darwin8.5.0]
D> YARVCore 0.4.0 Rev: 466 (2006-02-22) [opts: ]

 You have a problem with your version of yarv

uln% ruby -ve 'num = "+3"; p num, Float(num), Integer(num)'
ruby 1.9.0 (2006-02-24) [x86_64-linux]
"+3"
3.0
3
uln%

uln% ruby-yarv -ve 'num = "+3"; p num, Float(num), Integer(num)'
ruby 2.0.0 (Base: Ruby 1.9.0 2006-02-14) [x86_64-linux]
YARVCore 0.4.0 Rev: 475 (2006-02-23) [opts: ]
"+3"
3.0
3
uln%


Guy Decoux
Daniel H. (Guest)
on 2006-02-25 17:30
ts wrote:
>  You have a problem with your version of yarv

I was asking specifically about the second example:

num = "3e2"
Float(num)   # -> 300.0
Integer(num) # -> ArgumentError

> Guy Decoux

-- Daniel
ts (Guest)
on 2006-02-25 17:38
(Received via mailing list)
>>>>> "D" == Daniel H. <removed_email_address@domain.invalid> writes:

D> I was asking specifically about the second example:

 OK, sorry, I've not understood.

 Even 1.6.8 give the same result

svg% ./ruby -ve 'Integer("3e2")'
ruby 1.6.8 (2002-12-24) [i686-linux]
-e:1:in `Integer': invalid value for Integer: "3e2" (ArgumentError)
	from -e:1
svg%

 1.4.2 give a strange result :-)


Guy Decoux
Yukihiro M. (Guest)
on 2006-02-27 01:58
(Received via mailing list)
Hi,

In message "Re: Inconsistent Behavior Converting String to
Integer/Float"
    on Sun, 26 Feb 2006 00:07:36 +0900, Daniel H.
<removed_email_address@domain.invalid> writes:

|# Strange behavior
|num = "3e2"
|Float(num)   # -> 300.0
|Integer(num) # -> ArgumentError

Integer() does not understand scientific notation as an integer
representation.

							matz.
Daniel H. (Guest)
on 2006-02-27 02:22
(Received via mailing list)
I also find using a capatilized method name very odd and
un-rubyish. Looking through the docs, I see there is Array(),
Float(), Integer(), String(), URI(), DelegateClass(), and
possibly more. Why is this style used over Class#new?

-- Daniel
This topic is locked and can not be replied to.