I am looking for scripting language to replace Perl, which I am using
now. I don’t like Perl, because its awful syntax. I thought about
Python when I’ve heard of Ruby. I study Ruby now, it seems to be
excellend (everything is object) and I have one question.
Why do you have two classes of integers? I think there are only two
solutions for numbers in a good language:
-
Numbers can be as big as machine word. To use bigger ones module
should be included.
-
Nubers can be as big as programmer wants, but DON’T suply two
classes for the same THING. There should be one class to rule them all
– when number is small it is just one word, when it gets bigger it is
a list. Programmer wants to use numbers and don’t want to care about
implementation.
Ruby uses Duck typing, so in nearly all cases, Bignums are
indistinguishable from Fixnums; you wouldn’t know you had one until you
checked it’s class. The conversion is essentially transparent, and the
interface is the same. This basically makes it so that the programmer
doesn’t even need to think about the numbers, and how big they might be
getting; the interpreter takes care of all that for them.
If this is so in Ruby, then using two classes is not
good and that classes should be hidden to user (programmer). If Ruby
will have implemented BigNum, Fixnum, then to be complete it sould
implement BigFload (huge precission), Complex, Naturals…
These are actually numeric types that are defined in the standard
library. if your require ‘bigdecimal’ and ‘mathn’, you should get all
the numeric types available. Again, they are completely transparent:
rational numbers
1/4 * 2/3
#=>1/6
complex numbers
Math.sqrt(-1) + 1
#=>Complex(1, 1)
When you have included the module, rational numbers are created
whenever you divide an integer by an integer where it doesn’t come out
even. Taking the square root of any negative number will automatically
create a complex number for you.
I would like to be convinced that Ruby solution is good – now I don’t
see any reason for having two classes and I feel that some time in the
future this would be a problem. Good languages are good at the
beginning, but after few years they are patched, and patched – they
are getting worse and worse. (For example “use strict” in perl – it
seems to me as a patch that prevents programmer from doing something
wrong…)
The handling of Numeric types in Ruby seems to have been designed for
being resistant to patching problems. In fact, if you have a special
numeric type that you need to use, you can write your own! Inherit from
Numeric, set up how the math should work; wrap any appropriate methods
from Integer and Float so they generate your numeric type when specific
types of math are done, and you have it whipped. Once you load your
library, you can begin using the built-in numeric literals to do
whatever specific math you want, all transparently.
cheers,
–Mark
···
On May 16, 2004, at 8:23 AM, Leszek Dubiel wrote: