modules
DESCRIPTION
More notes on ModulesTRANSCRIPT
Ruby classes & Modules
--sowjanya Mudunuri
References
• The Ruby Programming Language by David Flanagan and Yukihiro Matsumoto
• Beginning Ruby by Peter Cooper
• Disclaimer: This notes is composed from the above referenced books. This presentation is used to help fellow friends who are trying to learn ruby.
Nested Classes• In Ruby its possible to define classes within other classes
• Nested classes are useful when a class depends on other classes but those classes aren’t useful anywhere else
class Calculator
def give_me_sum
Sum.newend
class Sum
end
class Difference
end
end
• Calculator::Sum.new
Modules
• Modules provide a structure to collect Ruby classes, methods and constant into a single, separately named and defined unit
• Useful to avoid clashes with existing classes methods and constants
• It helps to add the functionality of modules into your classes
Namespacing using Modules
• With two files of the same name or two methods of the same name sure will have coding disasters with unexpected functionality
• Modules help solve it by providing namespaces that can contain any number of classes , methods, and constants and allow you to address them directly
Example of Method in a Module
module Conference
def Conference.register
puts “I registered to a conference”
end
end
module Class
def Class.register
puts “I registered to a class”
end
end
Conference.register
Class.register
Info about Modules in Ruby
• You cannot define instances of a module since they are not classes nor they can inherit from anything
• They just provide ways to organize methods, classes, and constants into separate namespaces
Example of two classes with same name
module Admin
class Reporter
attr_accessor :name
end
end
module Customer
class Reporter
attr_accessor :name
end
end
a = Admin::Reporter.new
a.name
d = Customer::Reporter.new
d.name
Modules as Mix-ins• If you want to share some functionality between disparate classes you
can put those methods in a module and include that module into your class
• include takes a module and includes its contents into the current scope
module UsefulStuff
def class_name
self.class.to_s
end
end
class Ticket
include UsefulStuff
end
class Country
include UsefulStuff
end
u = Ticket.new
u.class_name
More on Modules
• Ruby comes with several modules by standard say “Kernel”
• “Kernel” module is included by the class Object
• Examples of Methods of “Kernel” module are puts, print, rand, chop, chomp. Find more about it
http://www.ruby-doc.org/core-2.1.0/Kernel.html
• Ruby also has “Enumerable” and “Comparable” modules which you can include in your classes
Exercise Time!!
• Go to IRB or http://rubyfiddle.com/
• Create a Module named ‘Pasta’ and define a method named ‘prepare’ in it and call the method on the Module
• Create another Module named ‘Vacation’ and define a method named ‘prepare’ in it and call the method on the Module
Exercise Time!!
• Create a Module named ‘GenericMethods’ and define a method named ‘my_class’. The method should return the name of the class. Define another method named ‘decorate’ and the method should print ‘&&&&&&&&&&’
• Now create a class named ‘Cake’ and include the module GenericMethods
• Now play in IRB or rubyfiddle how to call the methods ‘my_class’ and ‘decorate’ from ‘Cake’
Exercise Time!!
• Create a Module named ‘Chess’ and define a class named ‘Player’ and have an attribute named ‘name’
• Create a Module named ‘Sudoku’ and define a class named ‘Player’ and have an attribute named ‘name’
• Go to IRB/rubyfiddle and instantiate the ‘Player’ from Chess and Sudoku