本文介绍了在Ruby中,如何实现“20点”和“点-20”。使用coerce()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby中,操作

 点 -  20#将其视为点 - (20,20)
20点#处理它为(20,20) - 点

/ p>

但是下面的代码:

  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 - qx,@y - qy)
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)

pp - q
pq - p

pp * 3
p 5 * p

pp - 30
p 30 - p

输出:


$ b b

 #< Point:0x2424e54 @ x = -100,@ y = 
#< 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与p - 30相同

30 - p 实际上将被强制功能视为 p - 30



我真的很惊讶, - 方法不会强制参数方法:

  class Fixnum 
def - (something)
if(/ * something is unknown class * /)
a,b = something.coerce(self)
return - (a - b)#因为我们在做一个b,但我们想要b - a,所以它被否定
end
end
end

即函数返回否定版本的a - b ,而不是只返回 a - b

解决方案

减法不是一个交换操作,所以你不能只是交换操作数在你的 coerce 并期望它的工作。 coerce(something)应该返回 [something_equivalent,self] 。所以,在你的情况下,我认为你应该这样写你的点#coerce

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

您需要稍微更改其他方法,但我会留给你。


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.

解决方案

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.

这篇关于在Ruby中,如何实现“20点”和“点-20”。使用coerce()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 21:28