需要在我的类中编写比较点方法,称为point。红宝石

时间:2011-10-03 10:37:59

标签: ruby methods compare points

嗨我已经在ruby中编写了这个分数类,但是我需要一个比较方法类来向任何人提出任何想法从哪里开始?

class Point 
  attr_reader :x, :y

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

  def addpoint(x,y)   # used to add points 
    Point.new(@x+x, @y+y)
  end

  def to_s
    x.to_s+" , "+y.to_s # used to change from object to strings
  end
end

1 个答案:

答案 0 :(得分:1)

class Point
  def == p
     return false unless p.kind_of? Point
     x == p.x and y == p.y
  end
end