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 :
class SampleClass
# some code describing the class behavior
def sample_method
end
end
Instantiation:
An object instance is created from a class through the process called instantiation. In Ruby, it is created with Class method new.
Example:
std1 = Student.new(parameters)
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:
class Calculation
@x = 100
def add_num
@x = 200
end
def show_data
puts 'Value of x is : ', @x
end
end
instance = Calculation.new
instance.show_data
instance.add_num
instance.show_data
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
class MyData
def set_data(n)
@x = n
end
def get_data
@x
end
end
d = MyData.new
puts d.set_data(1000)
puts d.get_data
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:
class MyData
@@x = 1
def add_n
@@x = @@x + 10
end
def value
@@x
end
end
ins1 = MyData.new
ins2 = MyData.new
puts ins1.value
ins1.add_n
puts ins1.value
puts ins2.value
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 Employee
class << self; attr_accessor :instances; end
def store
self.class.instances ||= []
self.class.instances << self
end
def initialize name
@name = name
end
end
class Overhead < Employee; end
class Programmer < Employee; end
Overhead.new('Martin').store
Overhead.new('Roy').store
Programmer.new('Erik').store
puts Overhead.instances.size # => 2
puts Programmer.instances.size # => 1
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:
class Student
def self.no_students
puts 'There are five hundred students in my school.'
end
end
Student.no_students
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:
class School
def no_of_students
puts 'There are five hundred students in my school.'
end
private
def class_v
puts 'There are 70 students in class V.'
end
end
school = School.new
school.no_of_students
school.class_v
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:
class School
def no_of_students
puts 'There are five hundred students in my school.'
class_v
end
private
def class_v
puts 'There are 70 students in class V.'
end
end
school = School.new
school.no_of_students
Output :
There are five hundred students in my school. There are 70 students in class V.
Example : Protected access control
class Item
def initialize(price)
@price = price
end
def price
@price
end
def compare_price(c)
if c.price > price
"Second item's price is bigger."
else
"Second item's price is equal or less."
end
end
protected :price
end
item1 = Item.new(55)
item2 = Item.new(34)
puts item1.compare_price(item2)
puts item1.price
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:
class Student
def initialize(student_name, id)
@student_name = student_name
@id = id
end
def student_name
@student_name
end
end
std1 = Student.new("Sara", 5)
puts std1.student_name
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:
class Student
def initialize(sname)
@sname = sname
end
def sname
@sname
end
def sname=(new_name)
@sname = new_name
puts @sname
end
end
# you could the access @sname variable as
a = Student.new('Sara')
puts a.sname
a.sname ='Robin'
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:
class Student
def initialize(sname)
@sname = sname
end
attr_accessor :sname
end
# attr_accessor generates variable @sname accessor methods automatically:
a = Student.new('Sara')
puts a.sname
a.sname = 'Robin'
puts a.sname
Output :
Sara Robin
Example: attr_reader, attr_writer
You can do the same job using attr_reader and attr_writer methods :
class Student
attr_reader :sname
attr_writer :sname
end
s1 = Student.new
s2 = Student.new
s1.sname = "Sara"
s2.sname = "Robin"
puts s1.sname
puts s2.sname
Output :
Sara Robin
Ruby Class Constant:
Constants may be defined within classes, but unlike instance variables, they are accessible outside the class.
Example :
class ConstClass
Color1='Red'
Color2='Green'
Color3='White'
def show
puts "#{Color1} #{Color2} #{Color3}"
end
end
puts( ConstClass::Color1)
ConstClass.new.show
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.
class Student
def schooldetails
puts 'There are 700 students in our school.'
end
end
class Classfive < Student # < means inherit
def class5
puts 'There are 75 students in Class V.'
end
end
instance = Classfive.new
instance.class5
instance.schooldetails
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).
class Student
def schooldetails
puts 'There are 700 students in our school.'
end
end
class Classfive < Student # < means inherit
def schooldetails
super
puts 'There are 75 students in Class V.'
end
end
instance = Classfive.new
instance.schooldetails
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.
class X
class V
def ccccc
"Class V "
end
end
class VI < V
alias vcv ccccc
def ccccc
vcv + "VI "
end
end
class VII < VI
def ccccc
vcv + "VII "
end
end
puts V.new.ccccc
puts VI.new.ccccc
puts VII.new.ccccc
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
tutorial1 = String.new
def tutorial1.size
"Learn Ruby"
end
tutorial2 = String.new
puts tutorial1.singleton_methods
puts tutorial2.singleton_methods
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 :
tutorial1 = String.new
def tutorial1.size
"Learn Ruby"
end
puts tutorial1.size
puts tutorial1.class
#Here is another instance of String Class and call size method on it
tutorial2 = String.new
puts tutorial2.size
Output :
Size
You can also access the singleton class of an object using the following syntax :
class << object
Here is an example :
arra1 = []
class << arra1
def foo
"Hello World!"
end
end
puts arra1.singleton_methods
Output :
foo
Previous:
Ruby Methods
Next:
Ruby Modules and Mixins
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/ruby/ruby-classes.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics