In Ruby, how to implement “20 - point” and “point - 20” using coerce()?

寵の児 提交于 2019-12-08 11:42:46

问题


In Ruby, the operation of

point - 20     # treating it as point - (20,20)
20 - point     # treating it as (20,20) - point

are to be implemented.

But the following code:

class Point

  attr_accessor :x, :y

  def initialize(x,y)
    @x, @y = x, y
  end

  def -(q)
 if (q.is_a? Fixnum)
   return Point.new(@x - q, @y - q)
 end
    Point.new(@x - q.x, @y - q.y)
  end

  def -@
    Point.new(-@x, -@y)
  end

  def *(c)
    Point.new(@x * c, @y * c)
  end

  def coerce(something)
    [self, something]
  end

end

p = Point.new(100,100)
q = Point.new(80,80)

p (-p)

p p - q
p q - p

p p * 3
p 5 * p

p p - 30
p 30 - p

Output:

#<Point:0x2424e54 @x=-100, @y=-100>
#<Point:0x2424dc8 @x=20, @y=20>
#<Point:0x2424d3c @x=-20, @y=-20>
#<Point:0x2424cc4 @x=300, @y=300>
#<Point:0x2424c38 @x=500, @y=500>
#<Point:0x2424bc0 @x=70, @y=70>
#<Point:0x2424b20 @x=70, @y=70>        <--- 30 - p the same as p - 30

30 - p will actually be taken as p - 30 by the coerce function. Can it be made to work?

I am actually surprised that the - method won't coerce the argument this way:

class Fixnum
  def -(something)
    if (/* something is unknown class */)
      a, b = something.coerce(self)
      return -(a - b)   # because we are doing a - b but we wanted b - a, so it is negated
    end
  end
end

that is, the function returns a negated version of a - b instead of just returning a - b.


回答1:


Subtraction is not a commutative operation, so you can't just swap operands in your coerce and expect it to work. coerce(something) should return [something_equivalent, self]. So, in your case I think you should write your Point#coerce like this:

def coerce(something)
  if something.is_a?(Fixnum)
    [Point.new(something, something), self]
  else
    [self, something]
  end
end

You'd need to slightly change other methods, but I'll leave that to you.



来源:https://stackoverflow.com/questions/2801241/in-ruby-how-to-implement-20-point-and-point-20-using-coerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!