Define plus operator : Operator « Class « Ruby






Define plus operator


  class MyClass
    def initialize(string)
      @value = string.gsub(/[aeiou]/, '*')
    end
    def +(other)
      MyClass.new(self.to_s + other.to_s)
    end
    def to_s
      @value
    end
    def inspect
      @value
    end
  end

  a = MyClass.new("damn ")
  a += "shame"

 








Related examples in the same category

1.Define operator for class