Ruby Scripting
#3
Modules

What are modules in Ruby Ruby? If you come from a language like C++, you would say a module is a namespace.

What is that? Confused It is a container! Laughing

Yeah, the most basic explanation on modules would be that they contain stuff. I know, you are going to tell me now that classes also contain their own stuff. Confused The truth is that they do not need to be initialized like classes do in both languages. You could say they are floating in their own space.

Yeah, you could say that each module own its own boat and it keeps navigating the vast sea separated from all classes. It is not like it hates classes, it just can live and be invoked without them. Even so you can also mix them with classes in Ruby Ruby, something they have called a mixin. So yes, you can also use them as if they were an integral part of that specific class at will. Thus module ChipFlavors can be inserted in the Snack class to expand it by adding some sort of new toppings for all chips you can eat.

Code:
module ChipFlavors
 CHOCOLATE_CODE = 100
 MAYONNAISE_CODE = 101
 KETCHUP_CODE = 102
 MUSTARD_CODE = 103
 PEPPER_CODE = 104
end

class Snack
 include ChipFlavors
 def initialize
   @name = “”
   @price = 0
   @expire_date = “09-30-2019”
   @flavor = “”
 end
 attr_accessor :name, :price, :expire_date, :flavor
end

class Chips
 def initialize
  @name = “ChocoChips”
   @price = 2.50
   @flavor = “chocolate”
 end
end

We could add dozens of interesting chips recipes here but the point will remain the same. Our module, ChipFlavors, includes all chips flavor codes. This fact allows us to easily handle all of them in a single place without messing up with the actual classes that use them. Just imagine how tiresome it would become after a few years when we got like 50 different chips packages with different flavors.

Confused Checking every single of them would be harsh indeed. Centralizing some specific data makes it a bit easier to handle in a timely fashion today and even in the next couple of years. If we assigned one CONSTANT of all of them to every single class, we would probably take a long time to finish that process.

Actually for a machine it might be even better to just keep the module separate without mixing it in the Snack class. If you ever need to change codes, deprecate some and so on, you would only care about a module and not every single class every time you needed to update a vending machine.

Today we have learned three things. We got modules, mixins and one that was obviously different and still got pretty much ignored, CONSTANTS.

Ticked off Why do you keep yelling at me!?

O_o? I am not!

Shocked Yes, you are! Can’t you see all those capital letters?

Look Up Oh, those letters… Well, the thing is that I did that on purpose but with a different motivation. You see, they are called Constants and can be either be written like a Constant or CONSTANT. Actually, it IS a must to do that!

Ticked off Say what!? Now I need to keep yelling at other people?

Do not say that! We are not breaking some rule, we are following one indeed! You see, Ruby cares about how you type stuff on your computer. Use lowercase and it will treat that word or compound as either a variable or a method. Use @ at and it will think it is an instance variable. Type @@ and it will become a class variable. Still, if you use $ dollar symbol, it will not believe it is an amount but a global variable. Anything starting with an uppercase letter will be considered to be an actual Constant. It is part of what we call a programming language’s syntax. You could say it is like its own set of grammar and punctuation rules.

No matter if you respected your teachers at school or not, Ruby will force you to respect its rules! Go against what is common sense in the rubyist world and you will not get past that point ever. Laughing Really, it is a BAD idea! There are still some obscure ways to tweak its normal behavior, but no matter how good you think you are, DO NOT DO THAT! It is better to keep it as compatible with the default Ruby implementation as possible.

By the way, you mix in modules in classes by calling the include ModuleName right below that class name. You can include as many as deemed necessary.

Just keep this in mind, use CONSTANTS to permanently store data as long as your program is active. Do Not Change Their Contents after defining them! I mean, do not ever think you can do stuff like changing their contents one hundred lines of code later. If you defined them in a specific way, keep them like that for the rest of its execution.

Of course you can update it whenever you are maintaining its code while it is not running on any machine, but do it only then. If you just load it during its testing stage, it will not really matter. Just never do it while in its production stage (whenever you are already dealing with the “real world” audience).

Then why did you mix lowercase and uppercase?

O_O I did it because you can do so if you are defining a class or module. Use uppercase (and underscore once or twice) only if you are creating a class or module CONSTANT. I mean, when it belongs to that class or module.

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.

This is why I cannot stop repeating myself. Everything depends on your coding style at the end.

Is there anything else I might need to know about modules?

O_o? I am uncertain if you really need to know this, but Module is a class. Laughing + Tongue sticking out Yeah, even if it sounds weird, that is exactly what it is, just another Ruby class. What makes it important is that it does not ask you to initialize it and you can place it pretty much anywhere, and it keeps your code neat. For example, in my unreleased games KUnits and A Short Odyssey Gosu versions I keep language support stuff separated from scene classes to keep it easy to maintain. There I usually use the Lang or Terms module for such purposes. Yeah, there are many ways to deal with it but I just picked one of them because I felt like it. Laughing

Guess what? There is something else you might need to learn about them.

What is it?

Modules can contain more than just CONSTANTS. You can place floating methods there as well. That is not an official term, it just attempts to teach you they are not officially part of that module.

That’s impossible! You gotta be kidding me!!

O_o? Nope, I am not. The main idea behind it is that you should be able to do the same you have done with CONSTANTS as with any method you would like to include in one or more classes where you will include that module of yours.

So what happens to those methods after the mixin!?

They kind of become part of that class’s list of methods. Yeah, you could not distinguish them from the rest if you print the whole list! That is what makes them run smoothly as if they had always been there!

Are you saying modules will never own any methods ever!?

Err, nope, I never said so. There IS a way to make it own any specific method. You see, so far we have only seen that methods are called by typing the def keyword followed by some method_name. Normally that means that method will belong to a class. To change that in a module, we can add something special there. It is the self keyword!

Code:
module MyModule
 def self.method_name
   print “I belong to MyModule only!”
 end
end

Yes, it only belongs to that module now! In English it might be self evident what self actually means. For those speaking foreign languages (like your lazy servitor here), it might be strange to see how it is used. Honestly, it just means “I” or “me” own this method so call me first to access it properly. It is a simple replacement for that module’s name, meaning it will also work if you type that name instead. Since self is shorter and easy to understand, the convention is that you would just type self and not the module’s name there.

What if I’ve got plenty of module methods!?

Do not worry then! There is a simple way to avoid typing all those dozens of egotistic self.

Use module_function in a separate line and all methods written below will become module methods by default. Again, I told you Ruby Ruby lets you become lazy scripters. Laughing

Kyonides Wrote:There is also another way to make those methods become singleton methods (belonging to that module).
Just type extend self and it will take care of all of the methods defined beneath that line.
It has an advantage over module_function, it will let you use attr_ methods like those we have seen in my previous posts as if we were dealing with a class and not a module.

Keep in mind that such methods will NEVER become part of a class where you have mixed it in. You will still need to call it by typing MyModule.method_name every single time you need to trigger them there.

Right, I forgot to reveal another important fact about modules, you can call its module methods from anywhere! Once they have been properly defined, you can access them from any other script defined after that module has been defined.

Happy ArgumentError Message Debugging Nights! 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 1,148 07-15-2023, 05:52 PM
Last Post: DerVVulfman
Information  Assorted Ruby chm documents. hanetzer 10 35,248 08-17-2020, 04:19 AM
Last Post: kyonides
Brick  Learn Ruby! greenraven 2 7,614 05-16-2014, 12:25 PM
Last Post: greenraven
   Creating Your Own Scripting System DerVVulfman 3 7,467 10-12-2009, 03:37 PM
Last Post: Alpha-Mad
   How to make interesting NPC's without scripting Third333Strike 0 3,846 12-06-2008, 04:59 PM
Last Post: Third333Strike



Users browsing this thread: 6 Guest(s)