On Mon, May 10, 2010 at 6:39 AM, Colin Bartlett <colinb2r / googlemail.com> w=
rote:
> 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
> =A0attr_accessor :x, :y
> =A0def initialize(x,y); @x, @y =3D x, y; end
>
> =A0def -(q)
> =A0 =A0return Point.new(@x - q.x, @y - q.y) if q.kind_of? Point
> =A0 =A0Point.new(@x - q, @y - q)
> =A0end
>
> =A0def *(c); Point.new(@x * c, @y * c); end
>
> =A0def -@(); Point.new(-@x, -@y); end
>
> =A0def coerce(something); [Point::Coerce.new(something), self]; end
> end
>
> class Point::Coerce
> =A0attr_reader :value
>
> =A0def initialize(v); @value =3D v; end
>
> =A0def *(p)
> =A0 =A0puts "Point::Coerce #{@value} * #{p.inspect}"
> =A0 =A0Point.new(@value * p.x, @value * p.y)
> =A0end
>
> =A0def -(p)
> =A0 =A0puts "Point::Coerce #{@value} - #{p.inspect}"
> =A0 =A0Point.new(@value - p.x, @value - p.y)
> =A0end
> end
>
> p =3D Point.new(200,100)
> q =3D 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

While this explains the usage of the coerce method well, I'm not sure
that the answer to the original poster's question is mu*

I may be wrong here, it's early and I haven't yet had a full cup of
coffee, but while multiplication of a point (vector) and a scalar
makes sense, I'm not sure that there is a conventional  meaning to
subtraction (or addition) of a scalar and a vector.

Colin's solution treats a scalar in this case as a unit point
multiplied by the scalar.  If this is what's wanted it can be done
without the separate Point::Coerce class

 def coerce(something)
   [Point.new(something, something), self]
 end


* http://en.wikipedia.org/wiki/Mu_(negative)


--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale