Ruby is a pure Object Oriented language. That means everything is an object. Even the literals are objects. You can use dot notation with literals to call methods or properties of literals: For example
42.to_s == "42". If it looks weird to you it’s OK, because it is!
In HTML document, you can define a “class” for each element. This will help you create style in CSS that apply to multiple elements having the same “class”. This approach is very similar to object oriented programming.
The concept of a “class” represents a “category” of objects. We can describe an object with features and behavior, but the description is not the object itself. We can have multiple objects having the same description.
In Object Oriented programming, the description of object is called “class”. The object that comply with this description is called “class instance”. We can have many instances for a class. Each instance can be a bit different.
For example a “vase” has volume 1 liter, handle and color green while other “vase” can have 2 liters, no handle and color purple. Both objects are “vases”, but they have different attributes. In computer science a class can have “properties” while and object can have “attributes”.
In Object Oriented programming we have 4 main principles that must be available so that a language can be named OOP language:
- Abstraction: you can define abstract classes and interfaces,
- Encapsulation: you can define public and private properties and methods,
- Inheritance: one class can inherit properties and methods from a parent class,
- Polymorphism: one method in a parent class can be overwritten in a child class.
In Ruby a class can be derived from another class. All classes have a single root class named: BasicObject. When you define a class you can specify the base class using symbol “<“, this is very close to the mathematics symbol “<:” that represents a “sub-type”.
# define a simple class with constructor
class Dog < Object
@name = name # attribute declaration
def bark # method
puts "ham ham"
def breed=(breed) # attribute writer
@breed = breed
def breed # attribute reader
# first instance of Dog
dog1 = Dog.new("rex")
dog1.breed = "buldog"
# second instance of Dog
dog2 = Dog.new("bear")
dog2.breed = "labrador"
# use reader
- Super-class specification after class name: < Object is optional,
- Constructor method has special name initialize,
- Instance properties @name and @breed are private,
- Instance attributes can be accessed using a reader/writer method,
- Method reader has same name as one of the attributes,
- Method writer has same name as attribute follow by “=” suffix,
Observation: In other languages, instance variables (attributes) can be directly visible using dot notation. But in Ruby, instance variables can only be read or write using access functions: readers and writers. In other languages these two are known as “get” and “set” methods. If you are an idiot like me you will have hard time understanding this.
Warning: If you try to read an attribute by name using dot notation you will get an error. Ruby will try to find a method, with name equal to attribute name. If the method do not exist (you do not have a reader) then you get an error message.
Homework: Open this example live and run it: create class