Here's a script
class A def test p 'A' endendclass B < A alias :th_old_test :test def test th_old_test p 'B' endendVery simple: you have a class that does something, and you have a child class that inherits from it, and it does the same thing as its parent, along with some extra stuff. This is a common pattern.Now, I am treating the alias as a super. Because "test" is not defined in B, whatever method I'm aliasing is inherited.
So if you say
b = B.newb.testIt will show
Now the problem really shows when you start aliasing more things
Now we expect the code to read
but in fact it does not.Because this is how aliasing works.
Alias is not super. Alias can behave like super under particular circumstances, but overall, you're going to get bitten if you treat alias like super as I did.
The solution is to use super where you actually need a super call...but now we have a new problem:
If a method did not call super, perhaps it is better to assume it was meant to replace its parent's definition and try to not have anything to do with the parent.
But I believe most of the times, people just forget to call super?
class A def test p 'A' endendclass B < A alias :th_old_test :test def test th_old_test p 'B' endendVery simple: you have a class that does something, and you have a child class that inherits from it, and it does the same thing as its parent, along with some extra stuff. This is a common pattern.Now, I am treating the alias as a super. Because "test" is not defined in B, whatever method I'm aliasing is inherited.
So if you say
b = B.newb.testIt will show
Code:
AB
Code:
class A alias :th_a_test :test def test th_a_test p 'A2' endend
Code:
AA2B
Alias is not super. Alias can behave like super under particular circumstances, but overall, you're going to get bitten if you treat alias like super as I did.
The solution is to use super where you actually need a super call...but now we have a new problem:
I don't know of a way to address that problem.what happens when you are aliasing the same method multiple times, and you don't know whether super was called or not.
If a method did not call super, perhaps it is better to assume it was meant to replace its parent's definition and try to not have anything to do with the parent.
But I believe most of the times, people just forget to call super?
Last edited by a moderator:

