# Inheriting from / Delegating to Integer - Currency class implementation

**URL:** <https://www.ruby-forum.com/t/inheriting-from-delegating-to-integer-currency-class-implementation/128619>\
**Category:** Ruby\
**Created:** [February 9, 2008, 1:41pm UTC](https://www.ruby-forum.com/t/inheriting-from-delegating-to-integer-currency-class-implementation/128619 "2008-02-09T13:41:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pascal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9a140/32.png) [@pascal](https://www.ruby-forum.com/u/pascal)\
**Post date:** [February 9, 2008, 1:41pm UTC](https://www.ruby-forum.com/t/inheriting-from-delegating-to-integer-currency-class-implementation/128619/1 "2008-02-09T13:41:00Z")

</div>

Howdy folks.

I’m currently working on a Currency class which I’d like to behave  
exactly like Integer with some additional methods like #to\_currency and  
and overridden #to\_s method etc.

My first approach was to inherit from Integer directly which failed,  
because there is no constructor for this class. After some more  
research I found the DelegateClass method which seemed to be exactly  
what I need, however there was one major problem with it: Methods like  
#+ and #- returned Integer, not Currency objects. So "(Currency.new(20)

- Currency.new(30)).class" evaled to Integer and my custom methods have  
been gone.

The latest approach was not to inherit at all but write the class from  
scratch, carrying around a @value instance variable which stores an  
Integer representation of the currency.  
Unfortunately that idea didn’t work to good either because I couldn’t  
add Currency objects to Fixnums anymore, “20 + Currency.new(30)” raised  
a TypeError (“Currency can’t be coerced into Fixnum”), although my  
class implements a #to\_i method.

I actually like best the second way, using DelegateClass.  
Are there any ways to make the calculation methods return a Currency  
object then?  
Or do you at least have an idea how to solve the problem with the  
latest approach?

Thank you

– Pascal

---

<div class="post-metadata">

**Author:** ![pascal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9a140/32.png) [@pascal](https://www.ruby-forum.com/u/pascal)\
**Post date:** [February 9, 2008, 2:55pm UTC](https://www.ruby-forum.com/t/inheriting-from-delegating-to-integer-currency-class-implementation/128619/2 "2008-02-09T14:55:11Z")

</div>

On 2/9/08, Pascal E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #+ and #- returned Integer, not Currency objects. So "(Currency.new(20)
> 
> I actually like best the second way, using DelegateClass.  
> Are there any ways to make the calculation methods return a Currency  
> object then?  
> Or do you at least have an idea how to solve the problem with the  
> latest approach?  
> class Currency

attr\_reader :base\_value

def initialize(value)  
@base\_value = value  
end

def +(other)  
Currency.new(@base\_value + other.to\_currency.base\_value)  
end

def to\_currency  
self  
end

def coerce(other)  
[self, other.to\_currency]  
end

end

class Numeric  
def to\_currency  
Currency.new(self)  
end  
end

Currency.new(10) + Currency.new(5) # =\> #\<Currency:0x23ab4  
@base\_value=15\>  
10 + Currency.new(5) # =\> #\<Currency:0x238e8  
@base\_value=15\>

HTH

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![pascal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9a140/32.png) [@pascal](https://www.ruby-forum.com/u/pascal)\
**Post date:** [February 9, 2008, 3:30pm UTC](https://www.ruby-forum.com/t/inheriting-from-delegating-to-integer-currency-class-implementation/128619/3 "2008-02-09T15:30:27Z")

</div>

On 2/9/2008 Rick DeNatale wrote:

> def coerce(other)  
> [self, other.to\_currency]  
> end

Whoa, that looks really interesting.  
Thanks a lot, Rick!

However, is there another way to implement the Currency class using  
DelegateClass?  
It still feels wrong to me to write +, -, \* and / on my own, although  
they are exactly the same as in the Integer class.

– Pascal

---

<div class="post-metadata">

**Author:** ![pascal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9a140/32.png) [@pascal](https://www.ruby-forum.com/u/pascal)\
**Post date:** [February 10, 2008, 10:30am UTC](https://www.ruby-forum.com/t/inheriting-from-delegating-to-integer-currency-class-implementation/128619/4 "2008-02-10T10:30:37Z")

</div>

On Feb 9, 4:25 pm, Pascal E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> DelegateClass?  
> It still feels wrong to me to write +, -, \* and / on my own, although  
> they are exactly the same as in the Integer class.

Huh?.. And what is the result for $5 divided by $1? 😉

---

<div class="post-metadata">

**Author:** ![pascal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9a140/32.png) [@pascal](https://www.ruby-forum.com/u/pascal)\
**Post date:** [February 9, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/inheriting-from-delegating-to-integer-currency-class-implementation/128619/5 "2008-02-09T18:30:41Z")

</div>

On 09.02.2008 15:25, Pascal E. wrote:

> On 2/9/2008 Rick DeNatale wrote:
> 
> > def coerce(other)  
> > [self, other.to\_currency]  
> > end
> 
> Whoa, that looks really interesting.  
> Thanks a lot, Rick!
> 
> However, is there another way to implement the Currency class using  
> DelegateClass?

There might be but you will have to handle type differences anyway. You  
will have to implement #coerce, #+ etc. in any case. I recommend to use  
Rick’s approach because it is much cleaner.

> It still feels wrong to me to write +, -, \* and / on my own, although  
> they are exactly the same as in the Integer class.

Actually, they are not the same. Adding currencies is significantly  
more complex than adding ints. What happens if you add 5 EUR and 12  
USD? All sorts of issues are lurking here. A currency value is (at  
least) a decimal value and a currency identifier - at least this is the  
most straightforward definition I can think of. You are doing yourself  
a favor by doing it properly - it’s not that difficult.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![pascal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9a140/32.png) [@pascal](https://www.ruby-forum.com/u/pascal)\
**Post date:** [February 10, 2008, 7:34pm UTC](https://www.ruby-forum.com/t/inheriting-from-delegating-to-integer-currency-class-implementation/128619/6 "2008-02-10T19:34:07Z")

</div>

On 2/10/08, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 2/10/08, Alex S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> they are exactly the same as in the Integer class.
> 
> > Huh?.. And what is the result for $5 divided by $1? 😉
> 
> What, do you want me to do all of the OP’s work for him for free?

Actually, I rather blithely skipped over the real point in Alex’s  
question.

Although my code shows how to do coercion via double dispatching in  
Ruby, it ignores the domain question of what does it mean to add an  
integer to a currency.

$5 + 1 what?

It’s not clear that an integers and currency really SHOULD be  
compatible in this way.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![pascal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9a140/32.png) [@pascal](https://www.ruby-forum.com/u/pascal)\
**Post date:** [February 10, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/inheriting-from-delegating-to-integer-currency-class-implementation/128619/7 "2008-02-10T22:21:22Z")

</div>

Alex S. wrote:

> On Feb 9, 4:25ï¿½pm, Pascal E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > DelegateClass?  
> > It still feels wrong to me to write +, -, \* and / on my own, although  
> > they are exactly the same as in the Integer class.
> 
> Huh?.. And what is the result for $5 divided by $1? 😉

Hrm, I don’t know why my answer via google didn’t show up. Maybe a  
mistake on my part. So here again:  
That would be a mathematically sound: 5.  
If you need an example to see that this even makes sense, consider: You  
have $5 in $1 pieces. How many $1 pieces do you have? Answer: $5/$1 = 5.

With $1\*$5 it’s a bit different as powers other than 0 and 1 of $ as I  
can’t find anything where that’d make sense 🙂

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![pascal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9a140/32.png) [@pascal](https://www.ruby-forum.com/u/pascal)\
**Post date:** [February 10, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/inheriting-from-delegating-to-integer-currency-class-implementation/128619/8 "2008-02-10T19:28:45Z")

</div>

On 2/10/08, Alex S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > However, is there another way to implement the Currency class using  
> > DelegateClass?  
> > It still feels wrong to me to write +, -, \* and / on my own, although  
> > they are exactly the same as in the Integer class.
> 
> Huh?.. And what is the result for $5 divided by $1? 😉

What, do you want me to do all of the OP’s work for him for free?

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
