新手问题 tell me why?谁能告诉我为什么在方法<=>中为什么.instance_of?返回的是 false!

topswim · November 06, 2012 · 2120 hits

给 Point 类建立如下方法!可是方法的返回不是所希望的啊! irb(main):367:0* class Point irb(main):368:1> include Comparable irb(main):369:1> def <=>(other) irb(main):370:2> puts self.instance_of? Point irb(main):371:2> puts other irb(main):372:2> puts other.instance_of? Point irb(main):373:2> return nil unless other.instance_of? Point irb(main):374:2> self.x*2 +self.y2 <=> other.x2+other.y*2 irb(main):375:2> end irb(main):376:1> end => nil irb(main):377:0> p=Point.new(1,2) => # irb(main):378:0> q=Point.new(2,3) => # irb(main):379:0> p <=> q false # false => nil irb(main):380:0> p.instance_of? Point => true irb(main):381:0> q.instance_of? Point => true

No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.