The Basics of Creating and using Modules in Rails

Ruby on Rails is a wonderful open source full-stack web application framework favouring convention over configuration.

With reusable, easily configurable components normally used for creating applications, building applications in Rails is faster and easier resulting in improved productivity and business growth.

It is gaining traction with developers as it is flexible, scalable, and easy for web developers to write and maintain applications.

Ruby on Rails lays emphasis on to use known engineering patterns and principles in order to reduce the workload when building web applications.

Even though there are multiple methods to solve a programming challenge, Ruby professes to rely on commonly used patterns making Rails websites easier to maintain and upgrade.

Open source libraries with smaller blocks of Rails code solving particular problems are commonly used to reduce development overhead.

With the help of such code blocks, developers can insert stable, well-tested gems instead of spending time to write the same kinds of codes. One of the most powerful tools in a Ruby programmer’s toolbox is the module.

Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit.

This is useful so you can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes.

Creating a module: Writing a module is similar to writing a class, except you start your definition with the module keyword instead of the class keyword.

module MyFirstModule

def say_hello

puts “Hello”

end

end

As we can`t instantiate the module so for using the module we need to extend ar include the module into a class.

include : mixes in specified module methods as instance methods in the target class

extend : mixes in specified module methods as class methods in the target class

module ReusableModule

def module_method

puts “Module Method: Hi there!”

end

end

class ClassThatIncludes

include ReusableModule

end

class ClassThatExtends

extend ReusableModule

end

puts “Include”

ClassThatIncludes.new.module_method # “Module Method: Hi there!”

puts “Extend”

ClassThatExtends.module_method # “Module Method: Hi there!”

============================================================

Other Point about Module in Ruby:

1: ) Illustrating the basics of method lookup:

module M

def report

puts “‘report’ method in module M”

end

end

class C

include M

end

class D < C

end

obj = D.new

obj.report

Output:

#=> ‘report’ method in module M

Workflow: Suppose I am a Ruby object, and I’ve been sent the message “report”. I have to try to find a method called report in my method

lookup path. report, if it exists, resides in a class or module.

I am an instance of a class called D. Does D define an instance method report?

No.

Does D mix in any modules?

No.

Does D’s superclass, C, define a report instance method?

No.

Does C mix in any modules?

Yes: M.

Does M define a report method?

Yes.

Good! I’ll execute that method.

2:) Defining the same method more than once:

If you define a method twice inside the same class, the second definition takes precedence over the first. The same is true of modules.

module InterestBearing

def calculate_interest

puts “Placeholder! We’re in module InterestBearing.”

end

end

class BankAccount

include InterestBearing

def calculate_interest

puts “Placeholder! We’re in class BankAccount.”

puts “And we’re overriding the calculate_interest method!”

end

end

account = BankAccount.new

account.calculate_interest

Output:

#=>Placeholder! We’re in class BankAccount.

#=>And we’re overriding the calculate_interest method!

3:) Mixing in two modules with a same-named method defined:

module M

def report

puts “‘report’ method in module M”

end

end

module N

def report

puts “‘report’ method in module N”

end

end

class C

include M

include N

end

c = C.new

c.report

Output:

#=> report’ method in module N

4:) Including a module more than once

class C

include M

include N

include M

end

c = C.new

c.report

#=> report’ method in module N

Workflow:

You might expect that when you ran the report method, you’d get M’s version, because M was the most recently included module.

But re-including a module doesn’t do anything. Because M already lies on the search path, the second include M instruction has no effect. N is still considered the most recently included module:

5:) Going up the method search path with super: Inside the body of a method definition, you can use the super keyword to jump up to the next-highest definition, in the method-lookup path, of the method you’re currently executing.

module M

def report

puts “‘report’ method in module M”

end

end

class C

include M

def report

puts “‘report’ method in class C”

puts “About to trigger the next higher-up report method…”

super

puts “Back from the ‘super’ call.”

end

end

B

C

D

c = C.new

c.report

#=> ‘report’ method in class C

About to trigger the next higher-up report method…

‘report’ method in module M Back from the ‘super’ call.

RailsCarma has been working on RoR from its nascent stage and with a strong workforce trained in RoR, it has grown to become a highly trusted name in end to end Ruby on Rails consulting, architecture, building, management and extension to companies around the globe. 

Read more : 

Implementing TinyMCE in Ruby on Rails
Understanding Asset Pipeline Plugin
Polymorphic Associations With Active Record
A Simple Way To Increase The Performance Of Your Rails App

Get in touch with us.

Subscribe For Latest Updates

Related Posts

About Post Author

Leave a Comment

Your email address will not be published. Required fields are marked *