Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Ruby Scripting
#20
Some Oddities About new & initialize
And Should You Care About Types?

Ruby Ruby being an OOP has no specific operator for a new class alias a new copy of a class (its template). Instead it uses the new method that allows it to invoke that copy I was talking about.

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 Ruby Ruby works with objects mostly, it returns self instead after calling the initialize method.

Happy with a sweat What's self? - A Reminder

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 Ruby Ruby classes during a conversation if you like. Many programmers would understand you in a timely fashion.

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 Ruby Ruby and initialize would be considered its constructor for being its very own variable creation handler.

Thinking Now I wonder why Matz never called it construct or constructor instead being a C / C++ programmer himself. (The original documentation in Japanese has been translated to English so it might have got lost in translation. Happy with a sweat )

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

Thinking I guess I can come up with 3 reasons here. Its relatively easy to use by beginners and it can be customized by passing one or more arguments to its initialize method. Also its duck typing strategy makes you care more about what method you will call next than what type of object it actually is. If a given method does exist, Ruby will call it at once. You see, since it walks like a duck and quacks like a duck, it got to be a duck, right? Happy with a sweat

The problem is that it is not always the case! Shocked

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! Angry

And that is not really an issue at all. Laughing

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 Sad and it could crash.

And it will crash indeed! Laughing

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 Ruby Ruby things are different. CRuby does include types but are not supposed to be used that often according to several opinions found online. Nonetheless, there is no limitation as to how many times you should invoke it. Just keep them to the bare minimum.

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. Sad
Angry Darn Number 1! You have got quite an ego!
At least the RPG::Weapon would sound like Doug Marcaida Doug after testing a knife or machete or sword. Laughing

Happy with a sweat Here I used a case statement but it would also work with a typical if or unless conditional with their respective branches.

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. Shocked
"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,254 08-17-2020, 04:19 AM
Last Post: kyonides
Brick  Learn Ruby! greenraven 2 7,015 05-16-2014, 12:25 PM
Last Post: greenraven
   Creating Your Own Scripting System DerVVulfman 3 6,767 10-12-2009, 03:37 PM
Last Post: Alpha-Mad
   How to make interesting NPC's without scripting Third333Strike 0 3,504 12-06-2008, 04:59 PM
Last Post: Third333Strike



Users browsing this thread: