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 II - Strings
Ruby Tutorial II - Strings
Things to know:
The alphabet?
How long is a piece of string? Ruby knows. Do you?
A string is a group of text. Pretty simple, no? Here are a couple of examples:
- Code:
"hi"
'This uses inverted commas'
"This uses speech marks"
Now, take a look at this:
- Code:
'Hi. I'm IMP1'
Now, we wouldn't have this problem with speech marks, but we could also use the escape character (\). And now it'd look like this:
- Code:
'Hi. I\'m IMP1'
Here's another example of the escape character:
Input:
- Code:
"So you said \"Shut up\"! and I'm not even talking!"
"BackSlash!! \\"
"Yes\\No"
"Yes\No"
Output:
- Code:
"So you said "Shut up"! and I'm not even talking!"
"BackSlash!! \"
"Yes\No"
"Yes\No"
Now, just like numbers (not quite JUST like), strings can be added or multiplied!
Input
- Code:
"Hi." + " I'm IMP1"
"abc" + "def" + 'ghi'
"nom " * 3
"lo" * 4
Output:
- Code:
"Hi. I'm IMP1"
"abcdefghi"
"nom nom nom "
"lolololo"
Bare in mind when multiplying that the left side is being repeated (right hand side) times.
You also can't add a string to a number. It doesn't make sense.
So these won't work:
Input:
- Code:
3 * "Nom "
"Hi." * "I'm IMP1"
2 + '2'
'2' + 2
Output:
- Code:
TypeError: String can't be coerced into Fixnum
TypeError: can't convert String into Integer
TypeError: String can't be coerced into Fixnum
TypeError: Fixnum can't convert into String
There are (clearly) differences between numbers and strings:
Input:
- Code:
2 + 2
"2" + "2"
"2 + 2"
Output:
- Code:
4
"22"
"2 + 2"
That seems like a lot to take in, so no homework today
You can have the night off.
Good Hunting
NEXT TIME: Variables.
Last edited by IMP1 on Sun Jun 06, 2010 10:58 am; edited 2 times in total
IMP1- Coding Moderator
- Posts : 503
Gald : 1399
Stats
Cookies: 5
Re: IMP1 Ruby Tutorial
Once again, a brilliant tutorial
However, isn't there a difference between "" and ''
With "" you can use #{}!
However, isn't there a difference between "" and ''
With "" you can use #{}!
yami- Apprentice
- Posts : 127
Gald : 425
Stats
Cookies: 0
Re: IMP1 Ruby Tutorial
Yeah, but i was saving #{} for variables
IMP1- Coding Moderator
- Posts : 503
Gald : 1399
Stats
Cookies: 5
Re: IMP1 Ruby Tutorial
There's no addition for strings. It's called concatenation.
Also, you can concatenate integers (numbers) and strings by converting the integer into a string using .to_s method.
Also, you can concatenate integers (numbers) and strings by converting the integer into a string using .to_s method.
alundra120- Commoner
- Posts : 24
Gald : 119
Stats
Cookies: 0
OmegaDev :: Coding Plaza :: RGSS2 (VX) :: RGSS2 Tutorials
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|