Senior & Expert RoR Developers Discussion Forum by Nyros Technologies

HIRE Ruby on Rails Expert Developers Programmers Coders From India
Ruby on Rails PHP .Net Developers Community, Nyros Technologies, Kakinada
 
Log in  or IF not a member please REGISTER
Username:
Password:   


Keyword
Log in | Profile 

About Modules,Mixins and Namespaces

 
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> Experts Support & Chit Chat
View previous topic :: View next topic  
Author Message
swaminadhan



Joined: 14 Sep 2009
Posts: 73

PostPosted: Sat Mar 20, 2010 12:01 pm    Post subject: About Modules,Mixins and Namespaces Reply with quote

Hello developers !

Let us discuss about the very most important and powerful topics in ruby.


Let us discuss them briefly.


Modules :

Modules are the collection of classes,methods and constants. Modules groups classes together.


The syntax for modules is

module Hello (module_name)

end


The module name should start with an uppercase alphabet.


we can have multiple classes or methods in the modules. To acquire the methods of the module in a class we can include the particular module in our class.

for example:

Let us consider a module named "company" and a class "User" to access the methods and classes of the module in the class, we have to include the Company module in the user class.

class User

include 'Company' // ( Here we have included the company module in
the user class)
end



Now we can create an object of the class and we can invoke methods of the particular class.


Module can also contain methods,but since those are the instance methods we cannot access the methods directly, we have to include the module in a class and we have to create an instance to that class and we can access those methods.



Mixins :


According to the nature of Ruby, it will not support multiple inheritance, By including the module, the class will acquire the properties of that module. Since the module can have multiple classes and methods ,this can be taken as multiple inheritance.This concept is so called mixins.

As we said the module can contain many classes, module can also contain modules.We can include number of modules in a class.


There are 3 ways of include a module into a class they are.

let us take an example

module Cricket


end

The above module is in cricket.rb file

Similarly we have another module

module Soccer



end


lets say the above module is in soccer.rb file


If we want to acquire the properties of these two modules in an Sport class we can acquire in these 3 ways,they are as


1. The first way is

Class Sport

include Cricket
include Soccer

end


2. The second way is, in the file that Sport class contains require filename as

require 'cricket.rb'
require 'soccer.rb'

Class Sport

include Cricket
include Soccer

end



3. The third way is to

require Cricket
require Soccer

Class Sport

end.


Namespace :

The Namespace is nothing but the collection of classes, it is mainly used to avoid naming conflicts.


Normally when we are including some modules or by the other we may have methods with the same method name, So to avoid the naming conflicts we are using this namespace concept by adding the module name along with the method.


We have to declare as modulename.methodname


For example if we are including the person module in a User class.In person module if we have a method called name as below



module Person

def name

end

end



If we are including the person module in the user class, the user class will acquire the properties of the person module.


class User
include Person
def name

end

end

After we include the class will contain two name methods


So when we create a object to a class,the name method will get override,to avoid this we are using this name space.


To make this to treat these methods differently we use modulename.methodname, just like a class method.


class User
include Person
def Person.name

end

end


In this case these two methods are treated differently.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> Experts Support & Chit Chat
Page 1 of 1

 latest topics 
 Topics   Replies   Author   Views   Last Post 
No new posts validates_multiparameter_assignments plugin usage:
0 vijayalakshmi 27 Wed Jul 28, 2010 4:24 am
vijayalakshmi View latest post
No new posts paperclip plugin usage in rails:
0 vijayalakshmi 23 Wed Jul 28, 2010 4:07 am
vijayalakshmi View latest post
No new posts Polymorphism
1 phani.galla 46 Tue Jul 27, 2010 3:27 pm
umamahesh_nyros View latest post
No new posts Deploying Ruby on Rails Application with Passenger
2 sivakrishna.m 630 Tue Jul 27, 2010 2:16 pm
criether View latest post
No new posts Steps to implement star-rating using acts_as_rateable plugin
0 vijayalakshmi 46 Mon Jul 26, 2010 5:47 am
vijayalakshmi View latest post
No new posts Twitter Retweet Integartion
0 Anu 41 Thu Jul 22, 2010 12:27 pm
Anu View latest post
No new posts Usage of build method in rails
0 ktulasi 30 Thu Jul 22, 2010 12:19 pm
ktulasi View latest post
No new posts Facebook Like Integration in Rails
0 Anu 53 Thu Jul 22, 2010 12:16 pm
Anu View latest post
No new posts Fshare in your Rails Application.
0 Anu 39 Thu Jul 22, 2010 12:10 pm
Anu View latest post
No new posts Installation of Postgresql in windows.
1 swaminadhan 198 Thu Jul 15, 2010 10:17 am
proximity3 View latest post




Hire an expert Ruby on Rails developer / coder / programmer or development team from India now!!

Other Forums : PHP   ::   .Net   |   Free unlimited HTML CSS templates download

Nyros Technologies   |   Kakinada City Portal   |   Developers Blog   |   About Ruby on Rails Experts   |   More