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 Sun Jun 06, 2010 9:55 am

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

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"
There's no real difference between '' and "", each coder has their preferences.


Now, take a look at this:
Code:
'Hi. I'm IMP1'
The problem here, is Ruby mistakes the apostrophe for the end of the string. And Ruby's not gonna recognise the command 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"
Note you don't need to escape the ' because I used "".
Output:
Code:
"So you said "Shut up"! and I'm not even talking!"
"BackSlash!! \"
"Yes\No"
"Yes\No"
Two backslashes cancel out the escape powers, leaving behind a single backslash. The last two are the same as a \ won't cancel out a d.


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
Note the space before I'm IMP. That's because otherwise it'd print Hi.I'm IMP1.
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
And these will give the following errors:
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
along with the location of the error in the script.


There are (clearly) differences between numbers and strings:
Input:
Code:
2 + 2
"2" + "2"
"2  +  2"
Will result in three completely different things
Output:
Code:
4
"22"
"2  +  2"


That seems like a lot to take in, so no homework today Razz
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
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 yami Sun Jun 06, 2010 10:05 am

Once again, a brilliant tutorial

However, isn't there a difference between "" and ''
With "" you can use #{}!
yami
yami
Apprentice
Apprentice

Posts : 127
Gald : 425

Stats
Cookies: 0

Back to top Go down

IMP1 Ruby Tutorial Empty Re: IMP1 Ruby Tutorial

Post by IMP1 Sun Jun 06, 2010 10:49 am

Yeah, but i was saving #{} for variables Wink
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 yami Sun Jun 06, 2010 11:06 am

oh ok Razz
yami
yami
Apprentice
Apprentice

Posts : 127
Gald : 425

Stats
Cookies: 0

Back to top Go down

IMP1 Ruby Tutorial Empty Re: IMP1 Ruby Tutorial

Post by alundra120 Sat Jul 31, 2010 9:25 am

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.
alundra120
alundra120
Commoner
Commoner

Posts : 24
Gald : 119

Stats
Cookies: 0

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