From: Marnen Laibow-Koser Date: 2009-11-16T14:45:25+09:00 Subject: Re: Newbie question: Defining a numeric type Seebs wrote: > I have a type which has a bit of internal magic, but fundamentally, I > want > it to behave for most purposes like the value it yields from > to_i/to_int. Does it need to be a class of its own? You could use a module and just include it where necessary: module Magical def magic ... end end ... ... @val = 5 class << @val include Magical end @val.magic > > Basically, is there a way to avoid having to write +, -, etcetera, when > in each case it'd be: > def (other) > self.to_i other > end > If you don't like the first solution, try: [:+, :*, :-, :/, :%].each do |op| define_method op {|other| self.to_i.send op, other} end You could wrap this in a module too. > I think I'm thinking something like the Comparable mixin; sort of a > "I have a to_i, make me a number" module. > > -s Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org -- Posted via http://www.ruby-forum.com/.