Search code examples
rubycomparablefixnum

Why can I call the .between? method on a Fixnum?


For example, if I type the following in irb, it returns Fixnum.

20.class
=> Fixnum

Also,

20.between?(10, 30)
=> true

However, when I'm looking at the Ruby documentation, it says that .between? is part of the Comparable Module, which I didn't call yet.


Solution

  • It is because Fixnum includes Comparable.

    Fixnum.ancestors
    # => [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]
    

    Whatever is defined on an ancestor is available unless it is overwritten by a closer ancestor (or itself).