Integer Confusion

U

Uncle Roastie

We have class Numeric, class Integer, class Fixnum, and class Bignum.
It seems that literal numbers are converted to objects of Fixnum/Bignum.
It also seems that Numeric/Integer are meant only for inheritance and
not instantiation, yet they aren't abstract classes. Is there a write-up
on all this, the basic intention, the details? I find the topic just
glossed over in the Pick-axe book.

Thanks.
 
J

James Edward Gray II

We have class Numeric, class Integer, class Fixnum, and class Bignum.
It seems that literal numbers are converted to objects of Fixnum/
Bignum.
It also seems that Numeric/Integer are meant only for inheritance and
not instantiation, yet they aren't abstract classes. Is there a
write-up
on all this, the basic intention, the details? I find the topic just
glossed over in the Pick-axe book.

Fixnums is for small C-style computer numbers. Bignums are for the
totals that won't fit in a Fixnum, and Ruby will switch back and
forth as needed.

Integer is a parent for both of the above, so it's the holder for
shared behavior all integers should have.

Numeric is the root of ALL number classes, including Floats, again
for shared behavior.

The only time I've ever needed to know that that I can remember is
when I'm adding methods to one of them. If it's a method for
integers, add it to Integer. Methods for all numbers belong in Numeric.

Hope that helps.

James Edward Gray II
 
R

Robert Klemme

Uncle Roastie said:
We have class Numeric, class Integer, class Fixnum, and class Bignum.
It seems that literal numbers are converted to objects of
Fixnum/Bignum. It also seems that Numeric/Integer are meant only for
inheritance and not instantiation, yet they aren't abstract classes. Is
there a
write-up on all this, the basic intention, the details? I find the
topic just glossed over in the Pick-axe book.

Adding to James very comprehensible answer: Numeric and Integer are not
abstract simply because there are no abstract classes in Ruby. (Ok, there
are modules but in this case it doesn't fit the inheritance chain well and
no other class is likely to have to mixin numeric functionality.)

Regards

robert
 

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

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top