In Ruby, what the string "class ClassName <Base" means in this context

Given the code

require 'gdata' class Contacts class Gmail < Base 

What does this mean when we say "<Base", does this mean inheritance from the base class defined in the gdata module, in this case there will be no conflict with any other module that may also be required.

Or does it mean something else?

+4
source share
2 answers

The base does not really matter.

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

If the Base or Contacts :: Base class is not specified in gdata, this 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!" 
+1
source

A Gmail class is just a normal nested class definition that is a subclass of Base . Base seems to be defined somewhere in the gdata file, but nowhere does it say that gdata is a Ruby module, and if it was a module, you did not show that it was mixed (with include ) Contacts so I'm not sure if there are any conflicts to worry about.

0
source

All Articles