Sunday, 9 December 2012

Operator Overloading in Ruby

I expect many of you know that Ruby permits Operator overloading .Many of Ruby's operators are implemented as methods, you can define or redefine how these methods or  operators should work in your own classes , here i am trying to mention some fun facts that we can do with ruby.

I expect all of you should be knowing this , may be better than me as well . But this is a just a simple but strait forward example for the beginners like me,  those who wanted to experiment more on Ruby. 

Creating a class called ClassA

class ClassA
  attr_accessor :value
  def initialize(value)
    @value = value
     puts "Object of ClassA with value #{@value} has been created"
and Instantiating one obj of ClassA,
Object of ClassA with value 10 has been created
What if ruby permits a behavior like you can add objects of 2 different classes , then you are receiving the output without creating any scene , it will be fun right.