Assignment != Equality && Assignment != Identity && Equality != Identity
Let us review what an assignment actually is. It simply means that you pick either a Constant or a variable and set a value to that object and not any other around it. (Well, you could still do it but you got to have a valid reason for it or else do not even try to do that!)
The assignment operator is an = equal sign.
variable = actual_value
That means that whenever you use that variable or Constant, it will always return the last value you have assigned to that particular object. You should not alter a Constant's value, though.
Equality stands for something that has the save value as another object, be it a Constant or a variable.
Use two == equal signs to make the comparison between two objects that might be different or not.
Code:
CONSTANT = 1
@variable = 1
CONSTANT == @variable #=> Returns true
The === identity operator
And finally, we got the identity operator === to help us define if an object has been placed on both sides of a given comparison.
Here you got to be careful because it is excesively strict when looking for any identical value or object.
Code:
range = 1..8
range === 5 #=> Returns true
range === range #=> Returns... false!
range == range #=> Returns... true!
Object.new === Object.new #=> Returns false, they are two different Objects with their own IDs.
Whenever you are using the case when statement to double check if a give variable is equal to another, it will use the === identity operator by default.
As we could see in the first example of the identity checks, the first test returned a true-ish value because it used the === operator with EVERY SINGLE VALUE of that Range object until it hit number 5.
Honestly, you should use the == equality operator for if and unless statements mainly because there is no specific reason to do otherwise.
When using the case when statement, make sure you will be comparing the same kind of objects like numbers or classes. It will fail if you made a mistake and tested the value of a variable, like number 5, against a class like Integer, even if number 5 is an Integer object on its own right.
Other Operators
By the way, the != operator simply means Not Equal.
The !=== Not Identical operator does exist as well, but it would be quite weird for you to find a good use for it anywhere.
The && double et symbol you could find on the title of this very same post simply means and.
A Terrible But Good Looking Error That Can Make You Break Your Head
You will not be able to tell why it is not working as intended because it will make you think everything is fine.
Code:
variable == 100 || 50
Yeah, everything looks terrific and it NEVER throws any error at your face at all.
Really guys, it never will. 100% guaranteed!
Still, it is a huge mistake under most circumstances.
The reasoning behind it is QUITE SIMPLE indeed.
How do you know if the test is always working as intended if it never fails?
So tell me why is that a problem for any novice scripter?
It is a huge problem because you wound up defining a conditional statement you never needed.
I mean, if you always get a true-ish value back, why bother at all?
You could simply remove it and the script would work as usually does.
Keep in mind that any object other than nil and false are treated as true by default.
But I need it to reject certain requests because it did not hit the 100% accuracy I need in my skill, item consumption, etc.
If that is true, then you better delete the "|| number" part at once.
I cannot. I need it to also work if it hits the 50% mark...
You get a yellow card for making such a noob mistake then.
Actually, the only way that would work is to define the condition like I do it right below this very same line.
Code:
variable == 100 || variable == 50
I would suspect you come from a different programming language that allow you to do that or you simply watched some video tutorial that was not Ruby specific at all.
Stop mixing language syntaxes right there!
Don't do it ever again!
It is for your own benefit, anyway.
"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