开发者

Ruby includes returns false and nil

开发者 https://www.devze.com 2022-12-11 00:57 出处:网络
Can someone explain what the difference between false and nil this case is: irb(main):008:0> Fixnum < Integer

Can someone explain what the difference between false and nil this case is:

irb(main):008:0> Fixnum < Integer
=> true
irb(main):011:0> Integer < Fixnum
=> false
irb(main):012:0> String < Numeric
=> nil

I realize that "string开发者_高级运维s are not numbers" and that "not all integers are fixnums"

My thinking is naive and boolean. Either something includes something or not, true or false. But there appears to be a third option, like, "you are kidding, right?" ;-)

Can someone enlighten me?


The Object#< method seems to act like this, given the code A < B:

  • If A is 'higher' in the inheritance chain (eg, B.kind_of?( A ) == true) then true.
  • If A is 'lower' in the inheritance chain (eg, A.kind_of?( B ) == true) then false.
  • If A and B have no relation, then nil.

So, in your example. Integer inherits from Fixnum, obviously that implies that Fixnum doesn't inherit from Integer. And of course String has nothing to do with Numeric.

Here's some 'documentation' in the form of MRI source code :)

0

精彩评论

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