Which raises the question: Why is this
“non-OO”? Why not str.integer (for instance)
instead of Integer(str)? Is it just to make
it more universally available (i.e., to
different data types, Integer(non_str_obj))?
And why is Integer capitalized? I know of
only four methods in the entire Ruby core
that start with capital letters.
I sound like a five year old, do I not?
Always asking why…
Which raises the question: Why is this
"non-OO"? Why not str.integer (for instance)
instead of Integer(str)? Is it just to make
it more universally available (i.e., to
different data types, Integer(non_str_obj))?
Well, you can write
pigeon% ruby -e 'p Integer(12.4)'
12
pigeon%
And why is Integer capitalized? I know of
only four methods in the entire Ruby core
that start with capital letters.
Is this ‘jus the way it is’, or is there a reason? I suppose symbols get
hashed, but then why not apply the same logic to strings as well?
Something that isn’t naturally convertable to an integer causes
error. Symbols and nil are exception from historical reason, but
maybe they will be error in the future.
Thank you for the explanation of the mechanism. I am also wondering on the rationale for differences between then four Kernel methods and various
to_X methods. The same applies to the difference between to_str/to_s and
to_ary/to_a. Different tools for different jobs, but as to what those jobs
might be is a bit murky for me.
–
To assert that the earth revolves around the sun is as erroneous as to claim
that Jesus was not born of a virgin.
– Cardinal Bellarmine, “The trial of Galileo”, 1615
Unfortunately, it’s not possible to do this with user-defined data
types. If I have a String-like class with a to_i method that I want to
return 0 on error, then I can do that, but then Integer() will also
return 0 on error. If I want Integer() to raise an exception on error,
then to_i must also raise an exception on error.
It’s not good to have a standard library feature that user libraries
cannot emulate.
Paul
···
On Tue, Jun 25, 2002 at 12:27:03AM +0900, Yukihiro Matsumoto wrote:
Functional style methods are more strict than their OO style
counterpart in general.
Thank you for the explanation of the mechanism. I am also wondering on the
_rationale_ for differences between then four Kernel methods and various
to_X methods.
The same applies to the difference between to_str/to_s and
to_ary/to_a. Different tools for different jobs, but as to what those jobs
might be is a bit murky for me.
In message “Re: No exceptions from String#to_i” on 02/06/25, Paul Brannan pbrannan@atdesk.com writes:
Unfortunately, it’s not possible to do this with user-defined data
types. If I have a String-like class with a to_i method that I want to
return 0 on error, then I can do that, but then Integer() will also
return 0 on error. If I want Integer() to raise an exception on error,
then to_i must also raise an exception on error.
It’s not good to have a standard library feature that user libraries
cannot emulate.
How about Array(), Integer(), String() use to_ary, to_int, to_str
respectively [ruby-talk:43014], and Float() raises error if to_f
return NaN?
Sounds decent, but isn’t NaN technically a float? Also, since code that
handles floats should also be able to handle NaNs, it might be simpler to
just pass it along. Having it raise an error means that in some cases
there will have to be handling for both the exception from Float() and
handling for NaNs in general.
Also, why handle Float() differently? If because there is no to_flt as of
yet, then that holds for to_int as well…
– Nikodemus
···
On Tue, 25 Jun 2002, Yukihiro Matsumoto wrote:
respectively [ruby-talk:43014], and Float() raises error if to_f
return NaN?
Since it seems that core Integer, Float, String and Array are sort
of core classes in Ruby, it might also be nice to redefine Object’s
to_s, to_i, to_f and to_a to call the long form if it exists. This way the
long form would give the short one for free.
Something like:
class Object
def to_s
if respond_to? :to_str
to_str
else
inspect
end
end
def to_a
if respond_to? :to_ary
to_ary
else
[self]
end
end
def method_missing( message, *args )
case message
when :to_i then to_int if respond_to? :to_int
when :to_f then to_flt if respond_to? :to_flt
end
end
– Nikodemus
···
On Tue, 25 Jun 2002, Yukihiro Matsumoto wrote:
It seems a good idea. Let me think about it.
Will you submit RCR just for a record?
respectively [ruby-talk:43014], and Float() raises error if to_f
return NaN?
Sounds decent, but isn’t NaN technically a float? Also, since code that
handles floats should also be able to handle NaNs, it might be simpler to
just pass it along. Having it raise an error means that in some cases
there will have to be handling for both the exception from Float() and
handling for NaNs in general.
Good point. Hmm…
Also, why handle Float() differently? If because there is no to_flt as of
yet, then that holds for to_int as well…
Mostly because I didn’t see cases “to_flt” is useful.
matz.
···
In message “Re: No exceptions from String#to_i” on 02/06/25, Nikodemus Siivola tsiivola@cc.hut.fi writes:
In message “Re: No exceptions from String#to_i” on 02/06/25, Nikodemus Siivola tsiivola@cc.hut.fi writes:
Since it seems that core Integer, Float, String and Array are sort
of core classes in Ruby, it might also be nice to redefine Object’s
to_s, to_i, to_f and to_a to call the long form if it exists. This way the
long form would give the short one for free.
I don’t think it’s needed. It slows down the performance.