Comparisons
In Ruby you have got 3 ways to compare a value of a CONSTANT or variable with any other value.
if or unless statements (plus iterators (loops) like while and until)
Code:
if today == "Sunday"
print "It's Sunday!"
elsif today == "Monday"
print "How boring! It's Monday once again..."
else
print "I just have to drive off to work, you know."
end
Here we were using the == equality operator.
By the way, unless is a negative if statement.
case statement
Code:
case $game_party.actors[0].name
when "Aluxes", "Alex", "Alexis"
print "He's just an idiot!"
when "Basil"
print "A fool tailgating another fool..."
when "Hilda"
print "You should never trust a witch, you know."
end
As you can see in the example above, it's a convenient way to test things. It even allows you to add more than a single value to every single when clause. You could say it's like defining an if statement in a very convenient way.
But there's a catch!
The comparison is always made based on the call to the === identical method.
=== would return true.
=== would return false, even if they look kind of similar.
An if statement won't force you to use any specific operator for comparisons, you need to define them on every single line of code you're adding to your script.
Enter ? Ternary : Condition
What's a ternary condition?
Well, it's quite simple indeed. It's like an abbreviated form of an if statement. It can also be found in other languages like C and C++.
I guess you could find it useful if you need to test variables in order to find out if you set one value or another. You can also use it to call two distinct methods.
Setting a Variable's Value
Code:
legal_status = age >= 21 ? "Adult" : "Minor"
We used the >= greater than or equal to operator on the example above.
As you have noticed, the variable legal_status will return an Adult or Minor status based on a character's age.
Calling Different Methods
Code:
base_infiltrated? ? explode : business_as usual
In this case we have included three different methods to test if a hypothetical base has been infiltrated by the enemy.
If true, it will simply explode and ignore the third method.
If false, people will ignore the routine check and keep working hard at the military base.
Happy Boolean Return Values!
"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.
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.
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