在Ruby中,如何实现“20点”和“点-20”使用强制()?

发布于 2024-09-01 02:34:13 字数 1433 浏览 6 评论 0原文

的操作

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

在Ruby中,要实现

。但下面的代码:

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 实际上会被 coerce 函数视为 p - 30。可以让它发挥作用吗?

实际上,令我惊讶的是 - 方法不会以这种方式强制参数:

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

也就是说,该函数返回 a - b 的否定版本,而不是仅返回 a - b

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱没勇气 2024-09-08 02:34:13

减法不是交换运算,因此您不能只交换强制中的操作数并期望它起作用。 coerce(something) 应该返回 [something_equivalent, self]。因此,就您的情况而言,我认为您应该像这样编写 Point#coerce

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文