Let's say I have a base class with three sub-classes. The base class has a method common to most sub-classes, and it has an alias:
class Beer
def bottle_content
'250 ml'
end
alias_method :to_s, :bottle_content
end
class Heineken < Beer
end
class Stella < Beer
end
class Duvel < Beer
def bottle_content
'330 ml'
end
end
Now, if the to_s method is called on the diverging sub-class instance of Duvel, 250 ml is returned instead of 330 ml.
I get why; the alias is made at the level of the super class. And I know this can be fixed by re-defining the alias_method in the diverging class. But is there another way to do this?
Obviously, using a method for to_s would work:
class Beer
def bottle_content
'250 ml'
end
def to_s; bottle_content; end
end
But maybe there is more elegant approach?