Ruby Modules  Hot PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews Ruby
Written by Bogdan V   
Friday, 02 February 2007

{mos_sb_discuss:50}

Modules are similar to classes in that they hold a collection of methods, constants, and other module and class definitions. Unlike classes, you cannot create objects based on modules.


Modules serve two purposes:

First, they act as a namespace, letting youdefine methods whose names will not clash with those defined elsewhere.

Second, they allow you to share functionality between classes—if a class mixes in a module, that module’s instance methods become available as if they had been defined in the class.

Multiple classes can mix in the same module, sharing the modules functionality without using inheritance. You can also mix multiple modules into a single class.

Rails uses modules to implement helper methods. It automatically mixes the helper modules into the appropriate view templates.

For example, if you wanted to write a helper method that would be callable from views invoked by the store controller, you could define the following module in the file store_helper.rb in the app/helpers directory.

module StoreHelper
      def capitalize_words(string)
         string.gsub(/\b\w/) { $&.upcase }
     end
end

 


User reviews

There are no user reviews for this item.

Add new review




Powered by jReviews

Last Updated ( Sunday, 11 February 2007 )
 
< Prev   Next >