1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Jump Into Some Code

By

The following code illustrates this plainly. There are two modules, unimaginatively called ModuleA and ModuleB, each containing a method of the same name.

 #!/usr/bin/env ruby
 
 module ModuleA
   def ModuleA.message
     puts "Hello from ModuleA"
   end
 end
 
 module ModuleB
   def ModuleB.message
     puts "Hello from ModuleB"
   end
 end
 
 ModuleA::message
 ModuleB::message
 

The first thing that you'll notice is ModuleA or ModuleB has to be prepended to the method names in the definitions. The second thing you'll notice is that when you call these methods, you need to use a fully qualified name using the :: operator.

A fully qualified name is a module name and either method or class name separated by the :: operator. It unambiguously identifies a single method or class in a module. There's a shortcut around this though, but it may be counterproductive.

  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. Ruby's Basic Features
  6. Jump Into Some Code

©2014 About.com. All rights reserved.