Inconsistent Behavior Converting String to Integer/Float

D

Daniel Harple

$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
 
T

ts

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
 
T

ts

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
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Inconsistent Behavior Converting String to Integer/Float"

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

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

matz.
 
D

Daniel Harple

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top