Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Okay, welcome back. Now that you have Ruby installed, let's start writing some Ruby code.
So there's two ways to go about this. If we want some instant feedback, we want to try
some experimenting, try something out, we're going to use IRB. If we want to save a program
file and run it later, we'll write a Ruby program file. So let's start an IRB.
Okay, so to start IRB, we just use the IRB command, which ships with Ruby, so it was
installed when you installed Ruby. Now IRB stands for the Ruby Interactive Shell, and
it's what we call a read eval print loop. So it's going to read whatever we type in,
evaluate it as Ruby code, print out the results so that we can get real quick feedback, and
then it's just going to loop back around and ask us for more Ruby code. So let's give that
a try. We'll fire up IRB, and we get a little prompt. We can just start to type any Ruby
code. I'm just going to start with a basic Ruby string. Type in, Ruby is fun. And it evaluated
that code, and then the evaluation of that code is a string, so it just prints the string
right back out on the second line there. So let's go ahead and assign that to a variable.
Now one time saver in IRB is you can use up and down arrows as command line histories.
So if I use my up arrow here, I get the command back, and then I'm going to assign it to a
variable called comment, and then it evaluates that. The evaluation of that is the string
Ruby is fun. In fact, I can type in comment now. I've got that variable, and it's storing
the string. And if I hit return, I've got my string all stored in a variable. So let's
do something with the string. Let's say we wanted to up case it. We can just call the
up case method on that variable, and we get back Ruby is fun, all shouted in big capital
letters like that. So we can call methods inside of IRB. We can also use some of the
classes that are in the built-in Ruby standard library. So for example, if I wanted to know
what time it is, I could type in time.new. It's going to evaluate that, and it's going
to give me the current time.
Now let's show them a couple errors that they might run across while they're in IRB.
Yeah, there are a couple pitfalls in IRB to be aware of. Let's say you started a string
like Ruby is, and then you hit return before you ended it. Well, Ruby's just going to wait
here for a few minutes, waiting for us to type in the end of the string so I could say
fun, and now it prints that string. It's got a new line inside of it because I actually
typed a new line character.
Another one is maybe you start with the variable comment equals and hit return, and you see
that little question mark prompt there, so then we can finish that off. Ruby is fun,
and we get the same thing. Finally, you might just type in comment and leave out the E,
for example, and you'll get an error. It just says undefined local variable or method comment,
and because that's the last thing we typed in, this comment right here, well, sure enough,
that's the error. If you see error message inside of IRB, just take a minute to read
them. They're usually trying to help you out. Now, when you're all done in IRB, you can
either type exit or whatever the end of file character is on your operating system. Control
D would be that on the Mac here, so I'm just going to type exit, and I'm back out on a
command prompt. Don't worry about variables and classes and methods for right now. We
just wanted to show you how to use IRB and just do some basic Ruby inside of it.
Now, IRB is great for experimenting, and in fact, in every exercise, we'll have you go
and try some things and just give it a shot. Experiment around in IRB.
Yeah, the only problem with IRB is we can't save all that code that we typed in and then
rerun it. So to do that, we're going to have to create a Ruby program file.
Let's do that next.
Okay, so back here over on the terminal, I'm just going to clean things up a little bit
here. I'm in the Ruby Studio directory that I created, and it's empty. There are no files
inside of here, so we want to create a Ruby program file. Now, I'm going to use the TextMate
editor, and it has a command mate that's just going to let me create a new file and open
it up. And I'm going to call the file, helloRuby.rb. And then we got TextMate open, and now we
can just start to type some Ruby code.
Now in the IRB session, it automatically printed out the string. So if I had Ruby is fun, just
like that, in IRB, it would evaluate it and would print it out. Inside of a Ruby program
file, to print it out to the console, we have to use the command put s. And put s takes
a string, and it's just going to print that string out to the console. It's also going
to add a new line there. So I can save off the program file now, go back out to the command
line, and then to run the file, I type Ruby. That was a command that was installed when
you installed Ruby. And then the name of our file is helloRuby.rb. And sure enough, we
get Ruby is fun printed.
Let's also show them how to run it from within TextMate.
Well, that's a good idea, because I'm going to do this throughout the course, because
it's sometimes easier just to run it directly from inside of the editor, as opposed to jumping
out to the terminal. So to run it inside of TextMate, and your text editor probably has
some, maybe a different command, but most of them support some way to run Ruby. In TextMate,
I just use command r, and then I'm going to increase that font so you can see it. And
you see it printed, Ruby is fun right there.
Similar to what we did in IRB, let's go ahead and assign that to a variable. We'll assign
it to a variable called comment, and then we'll print out the upcase version of that
comment. So I'll run it again in TextMate. Sure enough, we get Ruby is fun.
So let's say we want to do that like three times. Well, we'll talk more about the times
method a little bit later, but three times in Ruby, it takes what's called a block, which
starts with a do and an end. And inside of that block, we're just going to print out
the upcase again. So we should get it printed four times here. And sure enough.
Ruby is four times as fun.
It's four times the fun of any other programming language. How's that?
Now don't worry about the syntax here. We're going to spend a whole bunch of time on blocks
in this course, so we're just kind of trying to get you a feel for what a Ruby file looks
like.
You know, in IRB, we also showed them about the time class. So it's probably worth showing
that we can print out the time here. We just do print as time.new, or put as time.new there.
And now we've got the time printed at the end of our Ruby fun little party there. And
then back out on the command line, of course, I can run helloruby.rb and we get the exact
same output. So now that we have this saved in the file, we can run it anytime we want.
Perfect. Now we looked at a few errors in IRB. Maybe we should look at those errors in our
program file too.
Yeah, let's see how to get out of sticky spots in our program file here. So we've got our
program file. We've got this comment. What happens if we leave off that trailing string?
We can already see based on the color coding here that something's sort of off. But if
we go ahead and try to run that, we're going to get an error. There's an un-terminated
string. So Ruby is reading to the end of the file trying to find that terminating quote.
So we can just stick that back in.
What if we say misspell comment right here and we run that? Well, we get the same error
we got in IRB. Undefined local variable or method comment. And we see that it's on line
two of our HelloRuby.rb file. So we can just go to line two here. Sure enough, we've got
a misspelling. We'll just clean that up.
And then finally, maybe we thought that there was a method on time called time.then. If
we try to run that, we're going to get an undefined method then. And again, it shows
us the line number. It's on line eight. So we can just go there and fix that up.
Ruby really tries to help you with the errors.
It does. It really pays to read the errors. Don't take them as something really scary.
Take them as something very helpful. Check the line number. And then, you know, fix things
up.
Right. All right. So now it's your turn to write your first bit of Ruby code. You're
actually going to write in both places, IRB, and then you'll make a Ruby program file.
And then in the next section, we'll look at numbers and strings. And that'll help you
be able to create the first player in the game.
That's right. All right. See you back here.
See you soon.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.