On Fri, 2006-04-28 at 23:39 +0900, kris wrote:
> If I have a class how do I get it to return a value without specifying a 
> method or attribute:
> 
> class Money
>   attr_writer :value
> end
> 
> 
> money = Money.new(5)
> 
> money.value => 5
> 
> but what if I want:
> 
> money => 5
> 
> So the actual object returns a value instead of a method or attribute, 
> like the base classes do.

Another way to get this kind of behaviour is:

require 'delegate'
Money = DelegateClass(Float)
m = Money.new(30.0)

p m
>> 30.0
p m + 30.0
>> 60.0
p m + Money.new(30.0)
>> 60.0
p m.class
>> Money
p m.__getobj__.class
>> Float
p m + "notmoney"
>> -:9: String can't be coerced into Float (TypeError)

-- 
Ross Bamford - rosco / roscopeco.REMOVE.co.uk