Classes are like the blueprint from which individual objects are created.
- Class variables
- Class methods
- Private Methods
Class Variables
- Class variables are used to store values related to a class in general rather than a particular instance.
- Class Variables have a class-scope.
- Class variables are typically used to store information regarding the class as a whole.
- A class variable looks like this “@@variable_name”.
- We can access our class variables anywhere in our class: in both class and instance methods.
- A class variable can be used to store a collection of instances of that class.
Class Methods
- Class methods are similarly used for implementing behavior that is related to a class in general rather than an instance.
- Class methods have a class-scope.
- Class methods enact behaviors that belong to the whole class.
A class method is defined like this:
def self.class_method_name
# some code
End
Class Constants
It's defined using all capital letters, like so:
class User
ROLES = ["Admin", "Moderator", "Contributor"]
end
The key distinction is that class constants are used to store data that doesn't change (is constant), while class variables are used to store data that does change. Scope-wise, class constants can also be accessed from outside of the class using this syntax:
Album::GENRES
Public, Private & Protected Methods
- Public methods are called by an explicit receiver.
- Private methods can only be called within the context of the defining class: the receiver of a private method is always “self”.
- Private methods are a way of encapsulating functionality within a class.
- Private methods also signal to other developers that this method is depended on by other methods in your program.
- Private methods are usually written with the word “private“ above them.
- Private methods restrict an outsider from calling methods that belong to an object.
Custom Errors
To build a custom error, we define an error class that inherits from the Exception class.
class PartnerError < StandardError
end
The raise keyword tells our program to raise our brand new PartnerError. The rescue allows our program to continue running.
begin
raise YourCustomError
rescue YourCustomError
end
“begin/rescue” syntax is used as another form of control flow to handle exceptions.
Object inheritance
- In Ruby, classes can inherit from one another. This means that they adopt all of the attributes and behaviors (i.e. all of the methods) of the parent, also called the super class.
- The use of inheritance allows us to create a family of classes with shared behavior, while still differentiating those classes.
- Inheritance provides an opportunity to reuse the code functionality and fast implementation time.
Steps for Inheritance
- Define the super class.
- Define the subclass
- Method overriding..(overwriting inherited methods)
# define a class
class Box
# constructor method
def initialize(w,h)
@width, @height = w, h
end
# instance method
def getArea
@width * @height
end
end
# define a subclass
class BigBox < Box
# add a new instance method
def printArea
@area = @width * @height
puts "Big box area is : #@area"
end
end
# create an object
box = BigBox.new(10, 20)
# print the area
box.printArea()
MODULES
A module is a collection of methods,constants and class variables. Modules are defined as a class but with the module keyword not with class keyword.
Important Points About Module
- You cannot inherit modules or you can’t create a subclass of a module
- Objects cannot be created from a module
- Modules are used as namespaces and as mixins
- All the classes are modules, but all the modules are not classes
- The class can use namespaces, but they cannot use mixins like modules
- The name of a module must start with a capital letter
Syntax:
module Module_name
# statements to be executed
end
Super
- What if there is a method in the parent class that we want our child to share some of the functionality of?
- Or what if we want our child class to inherit a method from the parent and then augment it in some way?
- We can achieve this with the use of the super keyword.
METAPROGRAMMING
Metaprogramming gives Ruby the ability to open and modify classes, create methods and much more. Metaprogramming allows us to build a class with dynamic attributes.
Benefits:
- Use of Keyword arguments makes our Ruby methods more flexible by allowing arguments to be passed in any order.
- Helps in writing DRYer, lighter, more intuitive and more scalable codes.
- We can dynamically add getters and setters methods
- We can make classes more flexible by adding any number of attributes based on our needs at the time without editing.
TOPICS:
Position Argument Keyword Argument Send Method Mass Assignment
### send()
The send method “send” a message to an object instance and it’s ancestors in class hierarchy until the method “reacts”, because the name matches the first argument. It dynamically calls on a method. It is used to assign attributes dynamically. This saves a lot of typing.