Another Bad Practice!?
It's Never Enough for Alias!
It's Never Enough for Alias!
Recently, a certain Wulfo brought to my attention that you can truly alias a parent class method in one of its child classes. The following test sample will prove it to you.
Code:
class A
def print_intro
print "I am the A class!\n"
end
end
class B < A
alias :print_new_intro :print_intro
def print_intro
print_new_intro
print "I am the B class!\n"
end
end
a = A.new
a.print_intro
b = B.new
b.print_intro
I was under the impression that it should fail immediately, BUT the thing is that it is ALLOWED by Ruby. I don't know why they would allow us to do it, yet, it's totally possible.
Just make sure you have declared the parent class properly. (You only need to do that ONCE in the script editor per child class.)
Thus, alias lets you alter both the child class or its parent class, mainly depending on which one you think it truly needs the aliasing. In this case B's parent class is A but we aliased it in the child class B.
Nonetheless, aliasing a parent class method when we already know that super method already exists and does the job without the issues alias experiences after a F12 reset, makes it totally illogical from a practical gamer's or developer's viewpoint.
But guess what? There are some caveats I should discuss here as well.
What if somebody else or a default script already calls that method in the child class and includes a super call inside? Especially important if one of us comes later on and wants to edit that function from a custom script.
Well, in such cases you can or should use an alias call there. At least if your plan is to keep the original method available for immediate use inside that child method. Of course, there's always the possibility that somebody else shows up and overwrites it and even calls the super method as if nothing had ever happened there.
Code:
class B
def print_intro
super
print "The name's B class!\n"
end
end
b.print_intro
If you ever thought that it would simply replace the B class's code above, you were right about that!
B's introduction has totally changed now.
And what happens if somebody else comes along and decides to alias it AFTER the latest piece of code has been included in a game project?
That's exactly the moment when things can get a bit ugly.
Code:
class B
alias :print_new_intro2 :print_intro
def print_intro
print_new_intro2
print "B, Class B...\n"
end
end
b.print_intro
Here you will see how our same old friend B class now prints A class's and B class's introductions plus the brand new one where it pretends to be some sort of scripted version of James Bond.
I told you beforehand that it could get messy, didn't I?
And yes, some times that would be the ideal outcome... unless you just pretended to call the parent method and not one of the previous definitions of the child method.
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE