06-05-2021, 08:20 PM
(This post was last modified: 11-28-2021, 03:32 AM by kyonides.
Edit Reason: Added an explanation
)
Variable & Constant Scope
Usually when learning OOP languages like Ruby, you end up meeting the need to care about variable scopes.
- $global scope
- @@class scope
- Initialized outside where no method can be found. - @instance scope
- Local to a class but can only be called by us if it's part of some method's code.
- local scope
- It can't escape a method's scope but can be passed on to another method as an argument.
- block scope
- It won't escape a block no matter what you do.
The only advantage the former offer is that they can be called from anywhere else via the :: scope operator.
The latter need you to define a method to grant them some access from the outside world.
Before we proceed with the next topic, we need to recall something I had said earlier on this thread concerning nesting classes and modules.
Kyonides Wrote:I have seen people nesting classes and modules at will. Why’s that?
Err, well, you could say they are free to do so. For instance, you could define a single module and then lots of classes inside… like the RPG module in the maker series. Stuff like RPG::Actor and so on are a “living proof” of it. Double colon :: mean the connection there is between the container module and its inhabiting class. To be honest with you, you can do that with two different classes as well. There the difference would be that the first class might call the second class as some sort of helper class that might contain stuff you deliberately wanted to separate from the main class. RPG::Event and its subclasses are an excellent example here. The sections you can see in the event editor might belong to either the main or the secondary classes.
So far it sounds quite straightforward, right?
The truth is that it isn't.
There's a catch!
You see, there are two ways to add a nested class or module to an existing one.
Basic Nesting
Code:
module Mod
class Classy
# Add some custom code here!
end
end
Specific Class Definition
Code:
class Mod::Classy
# Add some custom code here!
end
They look quite similar, right?
But they are not identical!
The truth is that the first one opens the module's scope first so you can add anything you can think off and later call it from anywhere inside that module or nested class without typing the whole address, i.e. you don't need to remind it of the Mod:: module name and the scope operator.
The latter behaves in a different way. Since it specified the scope from the very beginning, you can't directly access the module's methods, constants, etc. without specifying the scope as well. So you'd either need to include Mod (which is weird) or use the scope operator every single time you ever plan to call the constants' or any other object's values.
Why did this ever happen?
The reason is simple. You narrowed down the scope to only keep direct access to that nested class Classy. If you had used the first declaration, the scope would have been opened to the whole module instead. This would also include what you had defined in a different script section on the RM editor.
So choose the module or class scope wisely.
By the way, Constants in Ruby are treated almost like common variables.
The reasoning behind this would be that classes and modules got to be open for new additions at any given time.
Happy Refactoring of Your Code!
"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