Ruby Classes
Classes
In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior. In many languages, the class name is used as the name for the class (the template itself), the name for the default constructor of the class (a subroutine that creates objects), and as the type of objects generated by instantiating the class; these distinct concepts are easily conflated.
Class Definition
Classes in Ruby are first-class objects---each is an instance of class Class.
- Classes declaration is started with class keyword followed by a name.
- The name must begin with a capital letter and by convention names that contain more than one word are run together with each word capitalized and no separating characters.
- The class may contain a class variable, instance variable, and method, as well as calls to methods that execute in the class context at read time, such as attr_accessor.
- The class declaration is terminated by the end keyword.
Example :
Instantiation:
An object instance is created from a class through the process called instantiation. In Ruby, it is created with Class method new.
Example:
Instance Variables:
Instance variables are created for each class instance and are accessible only within that instance. Variables beginning with the @ character are 'instance variables'. Outside the class definition, using instance's public methods the value of an instance variable can be read or modified.
Example:
Output:
Value of x is : Value of x is : 200
The output of the above code shows blank (nil) and 200. @x is defined below class Calculation and is an instance variable to the class object whereas @x defined inside the add_num method is an instance variable belonging to instances of Calculation. They are two distinct variables and the first is accessible only in a class method (accessed in add_num and show_data methods).
Accessor Methods:
In the previous section, we have already seen that an instance variable can only be accessed or modified within an instance method definition. If you want to access it from outside, you need to define public accessor methods. Here is an example :
we've already found
Output :
1000
1000
Class Variables:
Class variables are associated with the class hierarchy and shared between a class, its subclasses, and its instances. A class variable must start with a @@ (two “at” signs). The rest of the name follows the same rules as instance variables.
Example:
Outputs:
1 11 11
Class Instance Variables:
Classes can have instance variables. This gives each class a variable that is not shared by other classes in the inheritance chain.
Class Methods:
The declaration of a Class method in Ruby is same way as normal method, except the class methods are prefixed by self or the class name, followed by a period. The class methods are executed at Class level and can be called without an object instance. They can access class variables but can not access instance variables. Here is an example :
Example:
Output :
There are five hundred students in my school.
Class Visibility: Public, Private and Protected
Ruby provides three levels of method accessibility, Public, Private, and Protected
- Public Method : By default, all methods in Ruby classes are public - accessible by anyone
- Private Method : This method can only be used by other methods inside the object in whose class it is defined.
- Protected Method : The method is visible to the methods of the current object and is also visible to objects of the same type when the second object is within the scope of the first object.
A protected method is like a private method in that it can only be invoked from within the implementation of a class or its subclasses. It differs from a private method in that it may be explicitly invoked on any instance of the class, and it is not restricted to implicit invocation on self.
Example:
Output:
There are five hundred students in my school. abc.rb:14:in `<main>': private method `class_v' called for #<School:0x12dcf68> ( NoMethodError)
In the above example school.no_of_students works, but school.class_v throws error as the private method can’t be called outside the scope of the class. Private methods can only be called implicitly, see the following example :
Example:
Output :
There are five hundred students in my school. There are 70 students in class V.
Example : Protected access control
Output :
Second item's price is equal or less. abc.rb:23:in `<main>': protected method `price' called for #<Item:0x142c9e0 @pri ce=55> (NoMethodError)
In the above example, we compare one Item instance with another Item instance. The comparison depends on the result of a call to the method price. The object doing the comparing (item) has to ask the other object (item2) to execute its price method. So, the price can't be private. Therefore we have called protected visibility. Item1 and item2 are both instances of the same class, with price protected rather than private, item1 can ask item2 to execute price. But if you try to call the price method of an item object when self is anything other than an item object, the method will fail (that's why it shows error).
Getter and Setter methods:
Instance variables are created for each class instance and are accessible only within that instance. But object's instance variables are not really private, you just can't see them. To access an instance variable, you need to create a getter and setter, 'getter' methods returns the value of a particular instance variable. Here is an example :
Example:
Output :
Sara
In the above example the student_name method. is the getter method.
'setter' method is used to modify the value of the instance variables from outside. Here is an example :
Example:
Output :
Sara Robin
In the above example we have defined setter method sname= and the instance variable @sname is set to the parameter new_name.
attr_accessor, attr_reader, attr_writer:
Ruby provides a couple of methods to do the above task easily : attr_accessor, attr_reader, attr_writer where attr_accessor will give you get/set functionality, the reader will give only getter and writer will give the only setter.The attr_accessor method creates both getter, setter methods and their instance variables. Here is an example :
Example:
Output :
Sara Robin
Example: attr_reader, attr_writer
You can do the same job using attr_reader and attr_writer methods :
Output :
Sara Robin
Ruby Class Constant:
Constants may be defined within classes, but unlike instance variables, they are accessible outside the class.
Example :
Output :
Red Red Green White
Inheritance:
Inheritance is a relation between two classes. A class can inherit functionality and variables from a superclass which is also referred as a parent class or base class. Ruby does not support multiple inheritances and so a class in Ruby can have only one superclass. In the following example, all functions and non-private variable are inherited by the child class from the superclass.
Outputs:
There are 75 students in Class V. There are 700 students in our school.
You can access the parent's method by using 'super' keyword if your class overrides a method from the parent class (superclass).
Outputs:
There are 700 students in our school. There are 75 students in Class V.
In a deep inheritance line, you can access parent class methods directly using access methods by alias.
Output
Class V Class V VI Class V VII
Singleton Classes:
In Ruby there is a way to define methods that are specific to a particular object and such methods are called Singleton Methods. When one declares a singleton method on an object, Ruby automatically creates a class to hold just the singleton methods. The newly created class is called Singleton Class. It is useful when you want to access that instance in different parts of the application, for example logging functionality, communication with external systems, database access, etc.
http://stackoverflow.com/questions/212407/what-exactly-is-the-singleton-class-in-ruby
Output:
Learn Ruby String 0
Singleton class is object specific anonymous class that is automatically created and inserted into the inheritance hierarchy.
You can use singleton_method to get the list of names for all of the singleton methods on an object. Here is an example :
Output :
Size
You can also access the singleton class of an object using the following syntax :
class << object
Here is an example :
Output :
foo
Previous:
Ruby Methods
Next:
Ruby Modules and Mixins