[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to override Numeric class

Bojan Mihelac

1/6/2006 4:35:00 PM

Hi all,
I am new to Ruby and am trying to overload "+" operator on Integer
class, without success.

class TestNum < Integer
def +(other)
self.value - other
end
end

puts TestNum.new(5)+5; # should return 0

I know that it should be simple, but.... any help appreciated.

--
Bojan Mihelac
Informatika Mihelac, Bojan Mihelac s.p. | www.informatikamihelac.com
-> tools, scripts, tricks from our code lab: http://source.m...



4 Answers

Robert Klemme

1/6/2006 4:55:00 PM

0

Bojan Mihelac wrote:
> Hi all,
> I am new to Ruby and am trying to overload "+" operator on Integer
> class, without success.
>
> class TestNum < Integer
> def +(other)
> self.value - other
> end
> end
>
> puts TestNum.new(5)+5; # should return 0
>
> I know that it should be simple, but.... any help appreciated.

Did you look at the error?

?> TestNum.new(5)+5
NoMethodError: undefined method `new' for TestNum:Class
from (irb):7
from :0

Your problem starts already with the creation... Integer is an abstract
class and I suspect it does something to #new.

You should also look at #coerce, see
http://www.rubygarden.org/ruby?CoerceE... for example if you want
to implement operators properly.

Kind regards

robert

James Gray

1/6/2006 5:03:00 PM

0

On Jan 6, 2006, at 10:35 AM, Bojan Mihelac wrote:

> Hi all,
> I am new to Ruby and am trying to overload "+" operator on Integer
> class, without success.
>
> class TestNum < Integer
> def +(other)
> self.value - other
> end
> end
>
> puts TestNum.new(5)+5; # should return 0
>
> I know that it should be simple, but.... any help appreciated.

Ruby provides the coerce method, for hooking into its math routines:

>> class TestNum
>> def initialize( value )
>> @value = value
>> end
>> attr_reader :value
>> def +( other )
>> if other.is_a? self.class
>> value + other.value
>> else
?> value + other
>> end
>> end
>> def coerce( other )
>> [self.class.new(other), self]
>> end
>> end
=> nil
>> tn = TestNum.new(5)
=> #<TestNum:0x31d2b8 @value=5>
>> tn + 5
=> 10
>> tn + tn
=> 10
>> 5 + tn
=> 10

Hope that helps.

James Edward Gray II



Bojan Mihelac

1/6/2006 6:57:00 PM

0

Thanks James. As I understand coerce method in Ruby classes is to assure
'other' is of right type and that operation could be performed. In this
example coerce is called from FixNum class, and '+' method is always
preformed in TestNum class, right?

--
Bojan Mihelac
Informatika Mihelac, Bojan Mihelac s.p. | www.informatikamihelac.com
-> tools, scripts, tricks from our code lab: http://source.m...



James Gray

1/6/2006 7:04:00 PM

0

On Jan 6, 2006, at 12:57 PM, Bojan Mihelac wrote:

> Thanks James. As I understand coerce method in Ruby classes is to
> assure 'other' is of right type and that operation could be
> performed. In this example coerce is called from FixNum class, and
> '+' method is always preformed in TestNum class, right?

Yes, that's correct.

James Edward Gray II