The Scripter’s Journey Series
7. The World of Modules!
Table of ContentsI. What are Modules?
II. Namespaces!!
III. Mixins!!
IV. Conclusion!
What are Modules? Alright, what are modules? Modules are like an organized
interface or common area where you can store methods and constants to use later in your program. What do I mean by this? Think of modules like toolboxes, where they have extra “tools” (constants and methods) you can use to build your house (program). They serve two key roles: creating a unique
namespace and allowing
mixins with classes.
Namespaces!! So…what’s a namespace? First of all, let’s first understand that Modules house two things: constants and methods (remember we learned what constants were last tutorial). Now, you’re probably wondering why there are only constants and not other types of “variables” like in classes…this is because modules do not create a new “type” like classes or can be inherited. You cannot make “instances” of a certain module like you can for classes. I want this to be ingrained in your head, modules are like “toolboxes” and can be used to easily add on to your program while classes are organized data types that can allow instances of this new type. So, with that out of the way, let’s go over namespaces.
Basically, namespaces allow you to create names of methods and constants without worrying that you may have naming conflicts. For example, let’s say you have two constants that both have the same name, but deal with two different things…like “ALLIANCE”, one constant refers to the Enemy Alliance (Evil), and the other refers to the Actor Alliance (Good). If you just go ahead and create two constants with the same name, this will cause problems. So, you can create two modules, one being a “toolbox” that contains basic actor information, and the other being a “toolbox” for basic enemy information:
CODE
module Actor
ALLIANCE = “Good”
def Actor.equip
#blah
end
module Enemy
ALLIANCE = “Evil”
def Enemy.gear
# Stuff
end
end
See? Now, notice something else I did in this module, I demonstrated how you would make your own methods. It’s the same way as making class methods in a class. Moreover, like I said, modules are like “toolboxes” and add on to the program…thus, modules and their contents can be used anywhere. Here’s how you would access a module’s contents:
CODE
# inside some random script
print Enemy::ALLIANCE
print Enemy.gear
And there you have it on namespaces! Next, mixins!
Mixins!!Mixins means the idea of
embedding a module into a class, or basically adding all the methods and constants of a module…and pouring it into a class for it to use freely. To do this, you must use the “include” keyword. Check out the following example:
CODE
module Actor
ALLIANCE = “Good”
end
class Game_Actor
include Actor
def initialize
print ALLIANCE
end
end
Notice that by using the “include” keyword, we no longer need to do something like this “Actor::ALLIANCE” but simply “ALLIANCE” because it’s essentially part of the class now. Get it? You can “include” as many modules as you like, thus “mixing it in!” Also, if you’d like to see some example in RMXP, for example, go to the help file and search up “String”, you’ll see under “Included Modules”,
Comparable and
Enumerable! You can check out those modules for yourself, but that’s just to show you some real examples!
ConclusionLol well that really short! But Modules are really important and plus I thought it would be good to have a breath of fresh air with this smaller tutorial verus last tutorial which was a moutfull. Anyway, like always, if you have any questions, concerns, or just feel like commenting, post! Thanks again and be back next! (Not sure what topic to go over next…but yea!)
The order of how these tutorials will progress:
http://www.rpgrevolution.com/forums/index....showtopic=54953~Law