- 720p
- 540p
- 360p
- 0.50x
- 0.75x
- 1.00x
- 1.25x
- 1.50x
- 1.75x
- 2.00x
We hope you enjoyed this lesson.
Cool lesson, huh? Share it with your friends
About this lesson
In this lesson we'll create our first Ruby program!
Quick reference
Hello World and c9 Transition
In this lesson we'll create our first Ruby program!
When to use
This is the first program that all coders create...the hello world program!
Instructions
Create a new file called hello.rb and put it in the rubycourse directory that you just created. Inside of that file write the code:
puts "Hello World"
save the file and head back to the terminal. From inside the rubycourse directory, type the command:
ruby hello.rb
Hints & tips
- Hello World is the first program most new coders learn
- puts "Hello World"
- 00:04 Let's start writing Ruby code.
- 00:06 So first things first, head over to your terminal.
- 00:09 And yours will look similar.
- 00:11 It'll have a different name here flat planet is the name of my PC.
- 00:14 No, I don't think the world is flat.
- 00:15 I just find it hilarious.
- 00:16 Some people do.
- 00:17 But whatever your username is, your computer name is, that will show up there.
- 00:21 So, the first thing we need to do is create a directory to hold all the files
- 00:24 that we're going to make throughout this course.
- 00:27 So type mkdir, and we want to put it in the C drive,
- 00:32 and let's call it rubycourse.
- 00:35 You can name it anything you want, but we're doing a Ruby course here.
- 00:38 Now, mkdir command will be the same if you're on Linux or a Mac.
- 00:42 But if you're on a Mac, obviously you're not going to type in C because Mac don't
- 00:46 have C drives, they have a different file structure.
- 00:48 Just go ahead and save it anywhere you want on your computer, but
- 00:51 make note of that so you can find it later.
- 00:53 Next, head over to our Sublime Text Editor.
- 00:56 And we want to go over to project, and add folder to project.
- 01:00 And then just navigate to your C drive, and find that directory we just created.
- 01:06 So we called it rubycourse there it is, single-click it to highlight it, and
- 01:09 click Select Folder.
- 01:10 And you see boom, it pops right up.
- 01:12 Now there's nothing in this directory yet.
- 01:14 So let's go ahead and create a file and save it.
- 01:16 So we've got a file that's already been opened, it's untitled.
- 01:19 So just head over here and click File > Save As.
- 01:23 And, again, navigate to your C drive and find that directory again,
- 01:28 that rubycourse, and double-click it.
- 01:31 And now let's name this file hello.rb.
- 01:36 And remember, all Ruby files end in .rb, it stands for Ruby.
- 01:40 That's how the computer knows that your program is a Ruby program, and
- 01:44 we're good to go.
- 01:45 Let's create a first Ruby program.
- 01:47 So just come over here and type puts and
- 01:51 in quotation marks Hello world.
- 01:55 Now go ahead and save this Ctrl + S on a PC, Cmd + S on a Mac or
- 01:58 you can just come over here and click File > Save.
- 02:01 Now we need to run this.
- 02:02 So let's head back over to our terminal.
- 02:04 And if we type in pwd, you can see we're currently in c/users/flatplanet.
- 02:10 And to run this file, we can run it from anywhere, but
- 02:13 it's easiest to run it from in the directory where the file is.
- 02:16 So we need to change into the directory cd change directory.
- 02:20 And we want to change into c/rubycourse, that's the directory we created.
- 02:26 And you'll notice you can see it right here, so we're definitely in there.
- 02:29 Or you could type in pwd and you see, yep, we're still in there.
- 02:33 We could type ls to list the stuff in this directory and
- 02:36 we see there's that hello.rb file.
- 02:38 So to run this, first I'm going to clear the screen here.
- 02:41 To run all ruby programs, we just type in ruby and then the name of the file.
- 02:45 So hello.rb.
- 02:48 When we do, you see a print on the screen.
- 02:50 Hello world!
- 02:51 Very cool.
- 02:52 Congratulation, you've just created your first ruby program.
- 02:55 Very, very simple and it's really just that simple.
- 02:58 Head back over to our sublime text editor and let's play around a little bit.
- 03:01 Let's add one more line here, type in system and then clear.
- 03:07 And then go ahead and save this.
- 03:09 And what this does is that it just clears this screen.
- 03:11 So let's head back over here and
- 03:13 I'm going to hit Enter a few times to kind of clutter up our screen.
- 03:16 Now let's run that file again.
- 03:17 So to run your file you can type in ruby and then hello.rb or
- 03:21 you can just hit the Up arrow key on your keyboard.
- 03:24 Ad that will just paste in the last command so that's kind of handy.
- 03:29 So, let's go ahead and run this. And boom,
- 03:31 you see it clears the screen first and then it prints out a Hello world.
- 03:35 So, that's just kind of a nice thing to do,
- 03:37 I recommend that you have that line at the top of all your ruby code throughout this
- 03:41 course just to make it easier to run and to read stuff from the terminal.
- 03:45 So very, very cool and that's our first review program.
- 03:47 In the future, if you want to add more files to our directory here,
- 03:51 you just right-click and click New File.
- 03:53 You could see it pop ups, and then we just go to File > Save As.
- 03:56 Let's call this secondprogram.rb.
- 04:02 That pops right up, we can close these.
- 04:05 In the future if we want to open them again, we just click on them, very cool.
- 04:08 If we want to delete one, we can right click and delete and it disappears,
- 04:12 very cool.
- 04:13 So we got the tools we need to program.
- 04:16 We've already written a very simple, very basic Hello World program,
- 04:20 and we're ready to go.
- 04:21 But before we move on in the course I should mention,
- 04:23 I'm going to be using a different development environment from here on out.
- 04:26 The thing that I'm going to be using is something called Cloud Nine.
- 04:30 And you can see here it is right here.
- 04:32 And this is just a simple tool that has all of these things that we've
- 04:35 already installed on one screen.
- 04:37 So you see, here's the text editor in the middle.
- 04:40 Here's my files on the left, and here's the terminal down here.
- 04:42 This is just an easier way to teach so that I have everything on the screen.
- 04:46 You actually can't use this program yourself.
- 04:48 Amazon bought this company and recently shut it down, so
- 04:50 it's not available anymore.
- 04:52 So you'll just follow along using the tools we've already set up,
- 04:55 your Sublime Text editor, and you Git Bash terminal.
- 04:57 So you know whenever I write something here,
- 05:00 you'll just obviously write it in your Sublime Text, right here, right?
- 05:05 Same exact code the same exact everything.
- 05:08 The files over here on the left hand side,
- 05:11 it will be the same files over here on your left hand side and Sublime Text.
- 05:15 And whenever I write a command down here in the terminal,
- 05:18 you'll write the exact same command in your Git Bash terminal, so
- 05:21 should be able to follow along, no problem.
- 05:24 Like I said, I just use this so it's easier to teach and
- 05:26 have everything on one screen.
- 05:28 So that's all for this video and the next video will look at comments and errors.
Lesson notes are only available for subscribers.