OmegaDev
Would you like to react to this message? Create an account in a few clicks or log in to continue.

IMP1 Ruby Tutorial

3 posters

Go down

IMP1 Ruby Tutorial Empty IMP1 Ruby Tutorial

Post by IMP1 Sat Jun 05, 2010 9:32 pm

First off, download the Ruby Installer. It'll allow you to try out ruby by yourself.

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
And Ruby can manipulate these numbers in ways you have never dreamnt of before! Unless you're a bit of a maths geek, and you have dreams about it.
Input:
Code:
1 + 1        # Addition
4 / 2        # Division
10001 - 10000 # Subtraction
4 * 3        # Multiplication
4 ** 2        # Powers
These will return, as you can probably imagine:
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
the result will actually be
Code:
1
3
as these are the amount of integers.

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
Even if the answer is a whole number, it'll still be a float. The answer will such look like this:
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)
These bad boys will result in this:
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
Phew, I hate long decimals.


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
IMP1
Coding Moderator
Coding Moderator

Posts : 503
Gald : 1399

Stats
Cookies: 5

Back to top Go down

IMP1 Ruby Tutorial Empty Re: IMP1 Ruby Tutorial

Post by Dezz123 Sat Jun 05, 2010 9:47 pm

Unless you're a bit of a maths geek, and you have dreams about it.
Damn, you caught me!
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. =.=
Dezz123
Dezz123
Legendary
Legendary

Posts : 1534
Gald : 6469

Stats
Cookies: 0

http://www.dezzgames.webs.com, www.evalon.webs.com

Back to top Go down

IMP1 Ruby Tutorial Empty Re: IMP1 Ruby Tutorial

Post by Kyle? Sat Jun 05, 2010 9:50 pm

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!
Kyle?
Kyle?
Platinum Cookiez
Platinum Cookiez

Posts : 500
Gald : 418

Stats
Cookies: 5

Back to top Go down

IMP1 Ruby Tutorial Empty Re: IMP1 Ruby Tutorial

Post by IMP1 Sun Jun 06, 2010 7:56 am

Just on the Trig functions, it deals in radians. Which is why it confused me.
IMP1
IMP1
Coding Moderator
Coding Moderator

Posts : 503
Gald : 1399

Stats
Cookies: 5

Back to top Go down

IMP1 Ruby Tutorial Empty Re: IMP1 Ruby Tutorial

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum