Search code examples
rubyclassinheritancemoduleradix

In Ruby what does the line "class ClassName < Base" mean in this context


Given the code

require 'gdata'

class Contacts
  class Gmail < Base

What does it mean when we say "< Base", does it mean inheriting from the Base class defined in the module gdata, in that case wouldn't there be a conflict with some other module that may be required too.

Or does it mean something else?


Solution

  • Base has no special meaning.

    ruby-1.9.2-p180 :001 > Base.inspect
    NameError: uninitialized constant Object::Base
    

    Unless a class called Base or Contacts::Base is defined in gdata, that example should generate an error.

    class Base
      def self.hello
        "oh hi!"
      end
    end
    
    class Base2
      def self.hello
        "ahoy!"
      end
    end
    
    class Contacts
      class Base
        def self.hello
          "hi 2 u"
        end
      end
      class Gmail < Base
      end
      class Gmail2 < Base2
      end
    end
    
    ruby-1.9.2-p180 :024 > Base.hello
     => "oh hi!" 
    ruby-1.9.2-p180 :025 > Contacts::Gmail.hello
     => "hi 2 u" 
    ruby-1.9.2-p180 :026 > Contacts::Gmail2.hello
     => "ahoy!"