[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Fixnum.new not working?

Daniel Schüle

5/28/2006 8:13:00 PM

Hello,

dumb question, why is it forbidden to use
i=Fixnum.new
also Bignum.new
and Float.new
and Complex.new

Regards, Daniel
2 Answers

Robert Klemme

5/28/2006 8:27:00 PM

0

Schüle Daniel wrote:
> Hello,
>
> dumb question, why is it forbidden to use
> i=Fixnum.new
> also Bignum.new
> and Float.new
> and Complex.new

These classes are special. Fixnums are "created" either through
literals (1) or through operations (1+2). Same for Bignum and Float.
You have to require 'complex' to use complex numbers:

irb(main):003:0> require 'complex'
=> true
irb(main):004:0> Complex.new
ArgumentError: wrong number of arguments (0 f
from (irb):4:in `initialize'
from (irb):4
from :0
irb(main):005:0> Complex.new 0,1
=> Complex(0, 1)


Kind regards

robert

Dave Burt

5/30/2006 9:49:00 AM

0

> Schüle Daniel wrote:
>> Hello,
>>
>> dumb question, why is it forbidden to use
>> i=Fixnum.new
>> also Bignum.new
>> and Float.new
>> and Complex.new

As they say, no such thing as a stupid question, only stupid people.
Just kidding, it's actually a good question.

Further to Robert's explanation, Ruby doesn't want you to worry about
creating numbers. Think of them as values rather than objects, and just
use them.

This lets the classes do smart things behind the scenes like only making
one object per Integer, or automatically reducing Rational numbers.

For Complex numbers and Rational numbers, you can use Complex(r, i) and
Rational(n, d) instead of new.

Cheers,
Dave