Understanding Ruby Eigen class as a Java Developer
When picking up ruby, It was quite difficult for me to understand the concept of eigen class. Understanding this required reading multiple articles, Hence this article is an effort to explain Eigen class using Java (Yes you heard it right, Explaining oranges using apples :) )
Example 1: Anonymous inner class in Java:
class Doable {
void method1()
}class Executor {
static Doable a = new Doable() {
void method1() {
System.out.println("Inside anonymous inner class")
}
void anotherSpecificToClass() {
System.out.println("Inside method specific to class")
}
} public static void main(String... args) {
a.method1(); // prints 'Inside anonymous inner class' Method method = a.getClass().getMethod(anotherSpecificToClass);
method.invoke(a): // prints 'Inside method specific to class'
}
}
In the above example variable a
is an object of a class which extends Doable
class. The class doesn’t have a name hence it is anonymous class.
In ruby, one of the way to implement such a code is, as follows:
class Doable
def method1
end
end
a = Doable.new
class << a
def method1
puts 'Inside anonymous inner class'
end
def anotherSpecificToClass
puts 'Inside method specific to class'
end
enda.method1 # prints 'Inside anonymous inner class'
a.anotherSpecificToClass # prints 'Inside method specific to class'
Example 2: Overriding methods in ENUM:
enum Color { RED {
String value() {
System.out.println("Red");
}
},
GREEN {
String value() {
System.out.println("Green");
}
},
BLUE {
String value() {
System.out.println("Blue");
}
} String value();}
In ruby the same way to do this using Eigen class is:
class Color
def value
end
endRED = Color.new
class << RED
def value
puts 'Red'
end
endGREEN = Color.new
class << GREEN
def value
puts 'Green'
end
endBLUE = Color.new
class << BLUE
def value
puts 'Blue'
end
end
Example 3:
class Car {
void static printNumberOfWheels() {
System.out.println("Number of wheels" + 4);
}
}class Executor {
public static void main(String... args) {
Car.printNumberOfWheels(); // Prints "Number of wheels 4"
}
}
In ruby the same way to do this using Eigen class:
class Car
class << self
def printNumberOfWheels
puts 'Number of wheels 4'
end
end
endCar.printNumberOfWheels // Prints "Number of wheels 4"
If we observe the above examples, In third we extended the self
(object) of the class with in the class. In the first and second example, we extended the the objects that we created and overrode the method. One deviation in first example is that we added a new method to the variable we created a
Conclusion:
From these observations what we can understand is:
1. Eigen class is for an object and not the the class. Third example may make us believe that Eigen class is for class. The answer is no, as even classes are objects in Ruby (100 % Object oriented).
2. There is no static methods/static fields. Car.printNumberOfWheels
calls the eigen class object (aka singleton object) of the class
Hence summarising Eigen class as follows:
Eigen class is similar to anonymous inner class in Java, with a distinguishing feature which comes because of Ruby. The distinguishing factor is that Eigen class is for the object and not for the class. Infact even the class is an object in Ruby