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 IV - Methods
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
- Code:
"7"
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
- Code:
0
7
13.0
0.0
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
All well and good, but hark!
Input:
- Code:
puts 'Wotcher! What's yer name?'
name = gets
puts 'What? ' + name + '? Funky...'
- Code:
"What? Bob
? Funky..."
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- Coding Moderator
- Posts : 503
Gald : 1399
Stats
Cookies: 5
Re: IMP1 Ruby Tutorial
Why are there two threads with the same name?
Also, good tutorial. Great to see more script tutorials.
Also, good tutorial. Great to see more script tutorials.
OmegaDev :: Coding Plaza :: RGSS2 (VX) :: RGSS2 Tutorials
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|