-2

I am trying to override the == operation as shown here. I created == as so:

class Point
  def ==(g)
    p'Never reaches here'
    if @x==g.grid
      true
    else
      false
    end
  end
end

I am calling the method as so:

def home? (gridPoint,point)
  if point==gridPoint
    do something
  end

When I run an application, "Never reaches here" is never printed, and the application skips the rest of the code and exits.

If I print gridPoint and Point right before the ==, I can see they are both points. Why does the program seem to have never entered the == method?

I tried point.==(gridPoint). This does not work either, and also defies the purpose of trying to clean the code.

Can anyone help me?

Community
  • 1
  • 1
Bawn
  • 509
  • 3
  • 13
  • 36
  • 1
    Hmm. It seems to work fine for me – Schylar Sep 26 '14 at 21:12
  • 1
    Working for me as well. The class `Point` should be capitalized, right? Might be an error in `gridPoint` or `point`, that they perhaps aren't instances of the class you declared. – jayelm Sep 26 '14 at 21:19
  • 1
    I think I'm missing sth. How do you guys can tell it is working for you, if the code supplied is incomplete? @Bawn, could you please update your question with some code which allows to reproduce the error - where does `@x` come from? What is `grid` method? What is `gridPoint` and how is it differnet form `Point`? – BroiSatse Sep 26 '14 at 21:41

1 Answers1

-2

I think I have some suggestions why your exact code doesn't work

  1. Class names should always be constants. This means you must capitalize the first letter!
  2. You need to declare your == method as a class method not an instance method.
  3. Along with #2 you need to call it using the class name not from the objects instance

class Point

      attr_accessor :x

      def initialize(string)
        @x == string
      end

       def self.== (g)
          p'Never reaches here'
          @x==(g) ? true : false
       end

      def home? (gridpoint, point)
        Point == (gridpoint)
      end
    end

    n = Point.new("Schylar")
    puts n.home?("Schylar","Schylar")
Schylar
  • 774
  • 1
  • 5
  • 13
  • 1
    No, I disagree with 2. and 3., which ruins the object-oriented aspect of the equals method. `==` should work just fine as an instance method. The code you've written above deviates strongly from what OP wants, and has several peculiarities - why is there a `point` argument for `home?`? – jayelm Sep 26 '14 at 21:31
  • I disagree in the statement that it ruins the OO aspect, but please write your version that works when wrote as an instance variable. – Schylar Sep 26 '14 at 21:39
  • Hm, after looking at OP's specific implementation of `==`, I see where you're coming from, but that really isn't a correct usage of `==` in the first place. I'm not sure what's wrong with OP's code, there really isn't enough information to diagnose the problem. – jayelm Sep 26 '14 at 21:42
  • I agree, OP needs to clarify with all of his code. Specifically @x and g.grid! – Schylar Sep 26 '14 at 21:48