09-08-2017, 04:00 AM
(09-07-2017, 04:54 PM)kyonides Wrote:DerVVulfman Wrote:I guess to get ALL of the math for 13/5, you would need to calculate 13/5 (to get 2) and 13%5 (to get the remainder '3').Or get a very specific result by dividing 13/5.0 #=> 2.6
If the last number is a float (a number with decimals), then you get the exact result at once, no need for remainders then. This situation would be useful if you want to calculate stuff like percents, but not that useful if you want to calculate in game item prices because nobody would expect decimals there.
Item ID 1 is a Potion, it's price is set to 50, adding 15% of taxes (yeah, expensive indeed) would make it cost 57.5 but you don't want that because... well, the party has gold pieces only, no silver coins, no copper coins, so you prefer to round it as much as to avoid decimals completely. In that case you would not just do this...
50*1.15
but...
(50*1.15).round
57.5 then becomes 57! round alters the previous algorhythm or calculation surrounded by parenthesis by removing all decimals.
Why did I multiply the price by 1.15? Because 1 represents 100% and 0.15 represents 15% in that calculation, also known as price plus taxes.
In Ruby there are two kind of strings, those enclosed by " " and others by ' '.
" " double quotes accept many special characters including % for substitutions, but single quotes are simple, they need \ to interpreter a third ' single quote as apostrophe or the stupid floating dash that appears in idioms like Siletrea's cat to show other people that such cat belongs to you. (Some memes would tell you otherwise implying the cat owns you according to the cat's own perspective XD) " " double quotes accept one or more ' single quotes inside and treats them as a quote (you're reporting what a neighbor told you last week, i.e. something about how dangerous a street has become since a gang took control of it) like in...
"My neighbor actually told me last week: 'those scumbags have taken over Elm Street and keep attacking clueless people that use it as a shortcut to save a few minutes...' It seems police is afraid of confronting them because the gang still own that street."
String substitutions, meaning you are gonna replace a weird character with an actual value (a number, a name), are defined like this.
CAT_OWNER = "%s's pet cat"
owner_name = 'DerVVulfman'
print sprintf(CAT_OWNER, owner_name) #=> "DerVVulfman's pet cat"
But let's say we had defined CAT_OWNER in a different way...
CAT_OWNER = "%s's pet cat has a weird name, it's %s."
owner_name = 'DerVVulfman'
cat_name = 'Patches'
print sprintf(CAT_OWNER, owner_name, cat_name) #=> "DerVVulfman's pet cat has a weird name, it's Patches."
sprintf means special print format, it's an adaptation of sprintf command found in C so that's why it kept it's weird name in Ruby even if it's not as readable as most commands in Ruby actually are. sprintf is used for string substitutions a lot depending on a scripter's taste. You can substitute as many characters (%s) as you defined in the original string. Keep in mind that the original string was not modified, sprintf made a copy of it with one or two names included.
There are more possibilities to substitute different stuff but I guess that one might help you a lot once you take the grasp of it.
DerVVulfman had said earlier Wrote:& thingie (not listed) ... we think of as the 'and' symbol is called an ampersand. Good for doing if... tests. 'if this & that.'
Why do we use & ampersand? Well, blame ancient Romans that defined the term "and" as et back in the days. Yes, it's an e plus a t mixed in a single symbol.
. Dots - well, they let us access methods or properties or attributes that belong to any object (a class, a module, an array, a hash, a variable, a number, even a string) if they already exist. Otherwise you'd get a NoMethodError message in no time because Ruby could not find it anywhere. (The only class where you can call a none existing method would be Struct, a special kind of class that can be altered at any time, but I think that's too complex for you now and we rarely use it anyway. It's like, err, combining a class with a hash so it lets you call and set any method, really, any method at anytime and returns nil if it just doesn't exist instead of the error message I mentioned above.)
.. Double Dots, ... Triple Dots - they are a different story, they are part of a Range! Range in Ruby means you set an initial value like 13 and a last value like 17 or 18 as the last value. Automatically you'll get every single value inbetween as well! There's a catch you gotta observe, double dots actually includes all those values (always numbers, no exceptions!), but triple dots exclude the last value.
0..18 #=> a Range representing all ages that would make a person be called a minor and get arrested if found drinking alcohol.
0...19 #=> same as above but skips 19
first_value..last_value #=> if first_value is 19 and last_value is 100 it will work like any Range that's defined with numbers.
countdown = 60 #=> let's say it represents 60 minutes here
first_value = 0
last_value = 120
range = first_value..last_value
range.include?(countdown) #=> true it IS included because first_value equals 0 and last_value equals 120.
every minute we substract a minute to our lovely countdown
countdown -= 1 #=> 59 minutes left because countdown - 1 would be 59
But that would be a mere calculation, to make the variable keep the new value we set it like this: countdown = countdown - 1 or countdown -= 1, they mean exactly the same, both will allow us to substract 1 minute from countdown.
if countdown == first_value
print "Siletrea's birthday party begins now!"
else
print "Siletrea should wait patiently..."
end
Yeah, I know, there's a second command named include... well, that's not completely accurate, it's called include? so Ruby thinks it's different from include. How can you be so sure? Well, use it to include a module like RPG and you'll get an error that will shutdown your game after you close the pop up window XD. include? with that question mark ? is a query or something like a question you make while holding a conversation with range. It's like asking range...
"Do you include 60, my dear friend range?"
Instead of getting a response like yes or not, range will tell you it's true or false only.
ohh! so you can round things to their nearest using .round?
how many commands like that are there? .something
new logo for Yesteryear created by Lunarberry!