From: Colin Bartlett Date: 2010-05-10T19:39:42+09:00 Subject: Re: how to implement 20 - point and point - 20 using coerce()? My apologies for the brevity and lack of explanation (I'm about to try to catch a train), but this might do the sort of thing that you want? (I think for coerce to work as you'd expect you need the operands(?) in the coerce array to be in the same order as in the calculation. Sorry if that doesn't make any sense! A fuller explanation will follow quite a bit later if you need one.) class Point attr_accessor :x, :y def initialize(x,y); @x, @y = x, y; end def -(q) return Point.new(@x - q.x, @y - q.y) if q.kind_of? Point Point.new(@x - q, @y - q) end def *(c); Point.new(@x * c, @y * c); end def -@(); Point.new(-@x, -@y); end def coerce(something); [Point::Coerce.new(something), self]; end end class Point::Coerce attr_reader :value def initialize(v); @value = v; end def *(p) puts "Point::Coerce #{@value} * #{p.inspect}" Point.new(@value * p.x, @value * p.y) end def -(p) puts "Point::Coerce #{@value} - #{p.inspect}" Point.new(@value - p.x, @value - p.y) end end p = Point.new(200,100) q = Point.new(80,70) puts p p, q puts p (-p) puts p p - q p q - p puts p p * 3 p 5 * p puts p p - 30 p 30 - p