Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Ruby Scripting
#6
Variable & Constant Scope

Usually when learning OOP languages like Ruby 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.
CONSTANTS belonging to any given class or module have the same scope as a @@class variable.
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.

Mad Scientist 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? Happy with a sweat
The truth is that it isn't. Laughing
There's a catch! Shocked
Indifferent 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

Happy with a sweat They look quite similar, right?

But they are not identical! Shocked

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? Confused

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. Laughing

So choose the module or class scope wisely. Nerd

By the way, Constants in Ruby Ruby are treated almost like common variables. Very happy + Tongue sticking out
The reasoning Thinking behind this would be that classes and modules got to be open for new additions at any given time.

Happy Refactoring of Your Code! Laughing
"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.

[Image: SP1-Scripter.png]
[Image: SP1-Writer.png]
[Image: SP1-Poet.png]
[Image: SP1-PixelArtist.png]
[Image: SP1-Reporter.png]

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! Laughing + Tongue sticking out

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
Reply }


Messages In This Thread
Ruby Scripting - by kyonides - 08-29-2019, 04:51 AM
RE: Ruby Scripting - by kyonides - 08-30-2019, 05:47 AM
RE: Ruby Scripting - by kyonides - 09-03-2019, 07:24 AM
RE: Ruby Scripting - by kyonides - 09-06-2019, 05:46 AM
RE: Ruby Scripting - by kyonides - 09-09-2019, 05:00 AM
RE: Ruby Scripting - by kyonides - 06-05-2021, 08:20 PM
RE: Ruby Scripting - by kyonides - 11-28-2021, 03:31 AM
RE: Ruby Scripting - by kyonides - 05-02-2022, 02:43 AM
RE: Ruby Scripting - by kyonides - 01-27-2023, 08:01 AM
RE: Ruby Scripting - by kyonides - 02-03-2023, 02:42 AM
RE: Ruby Scripting - by DerVVulfman - 02-03-2023, 03:52 AM
RE: Ruby Scripting - by kyonides - 02-03-2023, 04:33 AM
RE: Ruby Scripting - by kyonides - 04-19-2023, 12:47 AM
RE: Ruby Scripting - by kyonides - 04-19-2023, 01:12 AM
RE: Ruby Scripting - by kyonides - 06-08-2023, 09:24 PM
RE: Ruby Scripting - by kyonides - 06-08-2023, 09:46 PM
RE: Ruby Scripting - by kyonides - 06-12-2023, 06:29 AM
RE: Ruby Scripting - by kyonides - 07-14-2023, 10:37 PM
RE: Ruby Scripting - by kyonides - 07-15-2023, 07:32 AM
RE: Ruby Scripting - by kyonides - 07-16-2023, 06:05 AM
RE: Ruby Scripting - by kyonides - 07-17-2023, 04:01 AM
RE: Ruby Scripting - by kyonides - 08-22-2023, 08:59 AM
RE: Ruby Scripting - by kyonides - 08-28-2023, 03:34 AM
RE: Ruby Scripting - by kyonides - 11-18-2023, 09:02 AM
RE: Ruby Scripting - by kyonides - 04-16-2024, 06:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
   Ruby - Behind the Scenes DerVVulfman 0 617 07-15-2023, 05:52 PM
Last Post: DerVVulfman
Information  Assorted Ruby chm documents. hanetzer 10 33,253 08-17-2020, 04:19 AM
Last Post: kyonides
Brick  Learn Ruby! greenraven 2 7,012 05-16-2014, 12:25 PM
Last Post: greenraven
   Creating Your Own Scripting System DerVVulfman 3 6,765 10-12-2009, 03:37 PM
Last Post: Alpha-Mad
   How to make interesting NPC's without scripting Third333Strike 0 3,503 12-06-2008, 04:59 PM
Last Post: Third333Strike



Users browsing this thread: