07-16-2023, 06:05 AM
(This post was last modified: 07-16-2023, 06:12 AM by kyonides.
Edit Reason: 3 Reasons
)
Some Oddities About new & initialize
And Should You Care About Types?
And Should You Care About Types?

Why don't we check for a moment how new works in another language like C++?
The new Operator!
Code:
CPP_Class *pointer = new CPP_Class;
CPlusPlus.com Wrote:operator new can be called explicitly as a regular function, but in C++, new is an operator with a very specific behavior: An expression with the new operator, first calls function operator new (i.e., this function) with the size of its type specifier as first argument, and if this is successful, it then automatically initializes or constructs the object (if needed). Finally, the expression evaluates as a pointer to the appropriate type.
Since


self stands for the very own object. In this specific case it is the class being initialized or "constructed" there, if you prefer the C++ term instead. What this means is that in Ruby you can call initialize the constructor of

How that looks like in Ruby & CRuby?
Well, we already know the new and the initialize methods. new will invoke initialize by default, be it the internal implementation or a custom one you have provided in your script. Then initialize will return the final object, described here as self but representing pretty much any specific class depending on where does it belong to.
In CRuby you would call rb_class_new_instance which includes rb_obj_alloc (its allocator) before finally calling rb_obj_call_init (the function in charge of executing the initialize method of pretty much any Ruby class except for Array and Hash.)
So yes, there is some equivalent of the new operator in



What's the advantanges of using new and initialize in Ruby?


The problem is that it is not always the case!

Types & Custom Type Checks
RPG::Item and RPG::Weapon have both a method called name and another one named description. So you can call their name and descriptions methods and think for a moment that they are the same kind of object... but they are not!

And that is not really an issue at all.

You see, it comes handy for scripters to simply call an item's or a weapon's name method to display it on the window or sprite about to be drawn on screen.
What do we do if we need to get something like EVA in RPG::Armor ?
It does not exist in RPG::Weapon

And it will crash indeed!

What you do in this case is to check for types or classes.
In C and C++ types are declared in several places, be it in a function's return value or as part of declaring the arguments it will accept and so on. As you can see in our previous example, the class name is the type we were looking for.
In

In pure Ruby you can check classes instead of types.
Or like VX ACE implemented that, it creates a common method like actor? and then places a true or false boolean return value as a quick battler's class identifier. Keep in mind that both Game_Actor and Game_Enemy are nothing but specialized Game_Battlers by default.
Check Classes
Code:
def reply(value)
case value
when String
"SSShhh... tring!"
when RPG::Weapon
"It will ke'al!"
when RPG::Item
"Consume before: midnight"
when Integer
"I'm No. 1, you losers! :P"
end
end
Type reply(1) in a script call and it will soon call you a loser.


At least the RPG::Weapon would sound like



Just in case you did not know this, VX ACE runs this type of check whenever you try to retrieve or replace an actor's piece of equipment.

"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]](https://www.save-point.org/images/userbars/SP1-Scripter.png)
![[Image: SP1-Writer.png]](https://www.save-point.org/images/userbars/SP1-Writer.png)
![[Image: SP1-Poet.png]](https://www.save-point.org/images/userbars/SP1-Poet.png)
![[Image: SP1-PixelArtist.png]](https://www.save-point.org/images/userbars/SP1-PixelArtist.png)
![[Image: SP1-Reporter.png]](https://i.postimg.cc/GmxWbHyL/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!
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.
![[Image: SP1-Scripter.png]](https://www.save-point.org/images/userbars/SP1-Scripter.png)
![[Image: SP1-Writer.png]](https://www.save-point.org/images/userbars/SP1-Writer.png)
![[Image: SP1-Poet.png]](https://www.save-point.org/images/userbars/SP1-Poet.png)
![[Image: SP1-PixelArtist.png]](https://www.save-point.org/images/userbars/SP1-PixelArtist.png)
![[Image: SP1-Reporter.png]](https://i.postimg.cc/GmxWbHyL/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!

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