Ruby Scripting
#10
The Other Bad Practices Found in RGSS Scripts

We all should know by now how to declare an if or unless conditional statement. Happy with a sweat Yet, there are people out there that do weird stuff while doing so. In some cases the culprit was the RMXP's default code. For those guys that entered the scene after RMVX or RMVX Ace came out, I have no excuse. Confused

Code:
if variable == true
  print "It's true!"
end
if variable == false
  print "It isn't true..."
end

This is one of the most extreme cases seen so far.

For some reason, the guy checks the same variable twice, once for a truthy value and another for its opposite value. It is terrible! Angry

Just like in many other languages, there are other ways to check the same value appropiately, without repeating oneself.

Code:
if variable == true
  print "It's true!"
elsif variable == false
  print "It isn't true..."
end

There we can see how to define a second condition using elsif instead. It certainly means "else if" there.

If we were checking for totally different values, that solution would be fine. Even so, we are only checking what is its actual boolean value and under such circumstance, there is an easier way to deal with it.

Code:
if variable == true
  print "It's true!"
else
  print "It isn't true..."
end

Yes guys! That is all you needed to do from the very beginning! Laughing
Shocked But wait! There is more you need to learn here about those statements!

Code:
if is_variable
  print "It's true!"
else
  print "It isn't true..."
end

Grinning It is quite interesting how the code still works, don't you think? Thinking
But why does it work here? Who Knows?
There is an easy explanation. In Ruby, unlike C or C++, all objects return a truthy value if they are not equal to nil of the NilClass or false for obvious reasons. In C it would be quite common to see something like the following code:

Code:
if (result) {
  return "You were successful!";
} else {
  return "You failed!";
}

The negative way to declare that kind of statement would be:

Code:
if (!state) {
  return "LoadError: Failed to load script.";
}

A ! bang, yes, a ! bang there means negative value or opposite value or simply false.

And guess what? That does exist in Ruby as well! Shocked

Code:
if !is_done
  print "In the works!"
else
  print "Done!"
end

Of course, newcomers might need to include the equality operator and the true value like this == true but in the professional world that is not used at all. The only reason why you would ever, if ever, use it would be to test if an object is equal to true or false or nil in a threefold check. And this is extremely uncommon, guys! Happy with a sweat

Toggling Switches

So how can we apply that knowledge to changing the boolean value of a Game Switch? Thinking

Just use the following code:

Code:
$game_switches[1] = !$game_switches[1]

A Curious But Nonsensical Idea

Some time ago somebody wrote me a message telling me how dangerous it would be to do the following while using modules and classes.

Code:
module Some
  ONEISTHERE = "Someone is there!"
  class Thing
    include Some
  end
end

OK, I got to say that it is idiotic per se, still, this person wanted me to warn you about it. The reasoning behind it was that it would create a terrible loop that should be avoided at all costs. Interesting conclusion. Thinking

Mad Scientist Let us test this theory, guys!

[Image: attachment.php?aid=1723]

Nope, as we can see in the picture above, that is CERTAINLY NOT the case there!

What has happened was that we simply created a "new link" to the Constants already declared in the module above.
The only thing we have achieved was to come up with a redundant way to get to the same old Constant. Laughing
It was totally unnecessary for sure. Happy with a sweat Laughing

Happy Bug Bug Causing Nights!


Attached Files
.jpg   module_class_module_summary.jpg (Size: 67.21 KB / Downloads: 31)
"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,150 07-15-2023, 05:52 PM
Last Post: DerVVulfman
Information  Assorted Ruby chm documents. hanetzer 10 35,309 08-17-2020, 04:19 AM
Last Post: kyonides
Brick  Learn Ruby! greenraven 2 7,618 05-16-2014, 12:25 PM
Last Post: greenraven
   Creating Your Own Scripting System DerVVulfman 3 7,472 10-12-2009, 03:37 PM
Last Post: Alpha-Mad
   How to make interesting NPC's without scripting Third333Strike 0 3,847 12-06-2008, 04:59 PM
Last Post: Third333Strike



Users browsing this thread: 3 Guest(s)