开发者

writing a compare point method

开发者 https://www.devze.com 2023-04-09 23:08 出处:网络
im trying to write a compare point value eg does 1,0 equal 1,0 (true) this is what i have so far. any ideas?

im trying to write a compare point value eg does 1,0 equal 1,0 (true) this is what i have so far. any ideas?

class P开发者_开发技巧oint 

attr_reader :x, :y


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


def compare_point(x,y , a,b)   # used to compare points 

 if(x=a, y=b)

puts correct

else
puts wrong

 end
end

end


@current_location = Point.new 1,0


@start_location = Point.new 1,0

compare_point(@start_location,@current_location)


class Point
  attr_reader :x, :y

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

  def ==(another)
    [x, y] == [another.x, another.y]
  end
end

Point.new(1, 1) == Point.new(1, 1) #=> true
Point.new(1, 1) == Point.new(2, 1) #=> false

Note that if you use Struct you get accessors and equality for free:

class Point < Struct.new(:x, :y)
  # other methods here
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号