Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby includes returns false and nil

Tags:

class

ruby

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 "strings 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?

like image 824
teleball Avatar asked Jun 16 '26 09:06

teleball


1 Answers

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 :)

like image 185
rfunduk Avatar answered Jun 18 '26 21:06

rfunduk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!