IMP1 Ruby Tutorial
3 posters
OmegaDev :: Coding Plaza :: RGSS2 (VX) :: RGSS2 Tutorials
Page 1 of 1
IMP1 Ruby Tutorial
First off, download the Ruby Installer. It'll allow you to try out ruby by yourself.
Ruby Tutorial I - Maths
Ruby Tutorial I - Maths
Things to know:
In ruby anything after a hash symbol (#) on the same line will be ignored by Ruby and is generally used for making comments about the code.
Ruby can calculate maths for you! But you can't take it into an exam with you. I know, it sucks.
In ruby, there are two types of numbers: Integers, and Floats.
Integers are whole numbers, while floats have decimal points.
- Code:
3, 123, -368 #Integers
3.0, 0.0002, -6.5 #Floats
Input:
- Code:
1 + 1 # Addition
4 / 2 # Division
10001 - 10000 # Subtraction
4 * 3 # Multiplication
4 ** 2 # Powers
Output:
- Code:
2
2
1
12
16
Bare in mind that when working with integers, the answer will also be an integer, so if you had these:
- Code:
3 / 2
10/3
- Code:
1
3
Likewise, when inputting floats, the answer will be in float form:
Input:
- Code:
4.0 / 2
3 / 2.0
10.0 / 3.0
4.0 / 2.0
Output:
- Code:
2.0
1.5
3.333333333333333
2.0
Ruby can also do more complex mathamatics, such as square roots, and trigonometry and stuffs.
It also allows you to use Pi and E by way of a Math module (You don't need to know what a module is right now, only that it holds stuff like Pi so you can use it)
Let's take a look at what these inputs:
Input:
- Code:
Math::Pi # The constant Pi
Math::E # The constant e
Math.sqrt(4) # Square Root
Math.cos(60) # Trig function
Math.exp(2) # exponential (e ** number in the brackets)
Math.tan(45) # Trig function
Math.atan(1) # Arc Tan (opposite to tan)
Output:
- Code:
3.14149265358979
2.71828182845905
2.0 # sqrt (Square Root) returns floats
-0.952412980415156 # I thought it was a half, but google gives this answer too
7.38905609893065
1.61977519054386
0.785398163397448
Homework:
Try out some crazy Maths calculations, using parenthesis (silly name for round brackets)
Try these:
- Code:
1+((2**4)/Math::PI)
(Math::Pi + Math::Pi)/Math::Pi
Math.sqrt((2**2) + (4.0 / 2.0))
And
Do the spaces in this make any difference?
- Code:
1 + 1
1+1
1 +1
1+ 1
Good Hunting.
NEXT TIME: Strings.
Last edited by IMP1 on Sun Jun 06, 2010 9:59 am; edited 5 times in total
IMP1- Coding Moderator
- Posts : 503
Gald : 1399
Stats
Cookies: 5
Re: IMP1 Ruby Tutorial
Damn, you caught me!Unless you're a bit of a maths geek, and you have dreams about it.
Serious point: This is great, I've read it all! I'll read it again in a few hours to get it stuck in my head. =.=
Re: IMP1 Ruby Tutorial
By the way... Skype made the subtraction a phone number >.<
But there's not much here I didn'y know but the Pi and E is great for me!
But there's not much here I didn'y know but the Pi and E is great for me!
Kyle?- Platinum Cookiez
- Posts : 500
Gald : 418
Stats
Cookies: 5
Re: IMP1 Ruby Tutorial
Just on the Trig functions, it deals in radians. Which is why it confused me.
IMP1- Coding Moderator
- Posts : 503
Gald : 1399
Stats
Cookies: 5
OmegaDev :: Coding Plaza :: RGSS2 (VX) :: RGSS2 Tutorials
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|