Classes and Modules
Ruby class and modules
Classes and Modules
Ruby provides a couple ways to organize your application. You can put methods and variables into a Class, or you can create methods in a module that can be included in classes. Modules are similar to packages in other languages except that they can be added to classes as a type of mixin to provide extra functionality at runtime.
Class
A class is an instantiable unit that can contain variables and methods for interacting with data. Most of the time a class represents a part of your business model, a controller for request processing or business feature. In a class you can define attribute accessors which streamline the get / set and variable definitions for you.
Example class definition
class Person
attr_accessor :name
attr_accessor :birth_year
def age
current_date = Time.zone.now.to_date
current_date.year - birth_year
end
end
person = Person.new
person.name = "John Smith"
As you can see from the above the birth_year
is referencing the attribute accessor of the same name which internally uses a @birth_year
variable for the class. Attribute accessors add a setting and getting method to your class based on the name. The setter would be birth_year=
, where as the getter would be birth_year
.
To create a static method on a class you can refer to the class itself. Self
is a keyword in ruby that refers to the parent of the current scope. So when you are defining a class self
refers to the actual class, but inside a method self
refers to the instance of the class / object. Self is implied inside methods for method calls.
Example static method on a class
class Person
attr_accessor :name
attr_accessor :birth_year
def age
current_date = Time.zone.now.to_date
current_date.year - birth_year
end
def self.create
self.new
end
end
person = Person.create
person.name = "John Smith"
As you can see within the static method self
refers to the class to create a new instance.
Module
A module is allows you to create methods that can be used as helpers or to overlay on top of a class. You can also add classes to modules for namespacing similar to packaging. At runtime you can add methods to an instance of a class by including / extending a module. Refering to a module element requires the use of ::
to indicate the use of a namespace. The preceeding value before ::
is the module name and the suffix is the definition within the module, ie. method or class.
Example module
module Models
class Person
end
def self.create_person
Person.new
end
end
# reference the object inside the module
person = Models::Person.new
# or use the method on the Models module
person = Models.create_person
In certain cases, you’ll need to refer to a non-module class within a module, since the runtime assumes you are looking in the module itself. In order to do that you’ll need to use ::
without a prefix.
Example module referring to non-module
module Models
class Person
def role
@role ||= ::Role.new
end
end
end
class Role
end
person = Models::Person.new
person.role
Note in the above example referencing Role
without the ::
would also work since there isn’t another Role
defined in the example.