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 Wed Oct 27, 2010 5:46 pm

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

Ruby Tutorial IV - Methods

Things to know:
Anarchy is not synonomous with chaos.


Okay. So we have objects. Huzzah. But what if we want to do something with them. We have actually already seen some methods. Remember +, -, *, and /? I do. These are methods. They change, alter, or generally do something with an object. Alundra120 also mentioned another in his comment,to_s. The syntax (grammar/order of words) in ruby is object.method except for the mathsy ones. But let's look at to_s.

Here we have an integer. It's 7. But what happens if we use the method to_s?
Input:
Code:
7.to_s
Output:
Code:
"7"
It's turned it into string! Magic. But what if we liked it as an integer? Or what if we want to turn it into a float? Well, we'd use to_i or to_f.

Now we can add 2 and "3". But we need to convert either the string to an integer, or the integer to a string. It depends on whether you'd want to get "23" or 5.
But what would happen if we tried these:
Input:
Code:
"I'm not sure if this can be convered to an integer, imp...".to_i
"7 is supposedly a lucky number".to_i
"13 is actually lucky too.".to_f
"It's just silly mysogony".to_f
Output:
Code:
0
7
13.0
0.0
When there's a number at the beginning, Ruby can recognise it, and then convert it, ignoring everything later on, but when it's text, Ruby doesn't know which number you're talking about, so it picks zero. This is because nil as an integer is 0. Except we haven't encountered nil before, have we?

Nil is nothingness. Except we have to able to lable it nil, otherwise Ruby wouldn't know what the hell was going on. And we all know Ruby is a bit OCD. By default, any local or global variable is Nil. But we haven't done the different types of variables either. Well, onwards and upwards to victory! And variable types! And classes! But mainly victory!

One last method. It's like puts, except in reverse! Instead of putting a string, it gets a string. Like so:
Input:
Code:
variable = gets
puts variable
Output:Well now, this is different. Because variable will become whatever the user types into the command window. So what puts variable will also depend.

All well and good, but hark!
Input:
Code:
puts 'Wotcher! What's yer name?'
name = gets
puts 'What? ' + name + '? Funky...'
Output(assuming you put in Bob):
Code:
"What? Bob
? Funky..."
Oh No! the stuff after name is on a new line. gets must get the enter from entering it too! Never fear, a easy solution is at hand. The chomp method.

Try the same thing but using the line name = gets.chomp instead.


Short and sweet, like a grain of sugar. Or cinammon. Mmmm, cinammon. How I love thee.


Homework:
Try asking the user their age (don't forget to use to_i because gets gets a string) and then telling them that you are one year older, and then tell them your age.


Good Hunting.

NEXT TIME: Classes.
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 Wed Oct 27, 2010 6:43 pm

Why are there two threads with the same name?
Also, good tutorial. Great to see more script tutorials. Very Happy
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 Shady Wed Oct 27, 2010 6:58 pm

( I think theres like 5 )

Wow IMP these tutorials kick hardcore ass dude :O
Shady
Shady
Banned
Banned

Posts : 1434
Gald : 3288

Stats
Cookies: 0

http://www.slimshady.com

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