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

topswim · 2012年11月06日 · 2120 次阅读

给 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

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号