Getting Started Programming Lesson 2: Getting Ready to Rock
For Part 1 of this series, click here.
Now that we have a rough idea what it is we’re actually doing, we need to set up your computer so that you can actually run Ruby, the language we are going to be writing our code in.
Ruby is a wonderful language to learn to program in, mainly because it takes a lot of the tedium out of coding, allowing us to focus on the basics before diving into the deeper waters.
Now I started out writing out instructions for each and every platform, but I quickly realized that was going to be a complete mess. So instead, we’re going to be using a tool that will set you up with a virtual machine: Vagrant. For this, install Virtualbox on your computer, followed by Vagrant. Next, download this file, and extract it to a directory somewhere. This is going to be what we use to create your virtual machine, with everything we need to get you started in a nice Linux box. While you can natively install Ruby on all platforms, it does not work well with Windows due to the fact that Ruby can include some low level code that won’t work on Windows. Trust me, I’ve tried, and this is the easiest way to do Ruby development on Windows, so that’s what I’m going to have everyone do.
Once you’ve extracted the files somewhere on your computer. Open up a terminal by doing one of the following:
- Windows: Click the start menu, type in “cmd” into the search bar, select the first one.
- OSX: Click the Launchpad, find the Utilities folder on the first page, and select Terminal
- Linux: Man I seriously hope you know where your terminal is if you use Linux. If not, hit me up with what window manager you’re using and I can tell you how to find it 😉
Since you’ve never programmed, likely we just entered totally new territory, but don’t panic! The terminal won’t bite, and you can’t do anything really bad with what I’m going to have you do.
When you opened the terminal, you did the equivalent of opening up a file explorer window. However, it probably isn’t where the folder you extracted is. We need to move the terminal there, similarly to how you click into folders to get where you want in the user interface. For that, we’ll do the following: type in “cd” followed by the path to your folder, with a slash to represent clicking a folder. For instance, Windows users will type “cd C:\Users\Me\Desktop\folder” to get to their folder, while Mac and Linux guys will type something like “cd /home/me/Desktop/folder”. Again, if you get into trouble, drop me a line and we can work it out. Don’t be scared to ask a question. We all have to start out somewhere!
Now since we’re at the folder where you extracted the downloaded files, and everything is installed and ready to go, all we have to do is type “vagrant up” to create our server. What you’ll see is your terminal fill with stuff that it’s doing. What this does is make a copy of the image we just downloaded, turn it on, and start installing all the software for you. The cool part is if you mess something up, all you have to do is reset the Vagrant vm and it will rebuild all the stuff for you good as new. So feel free to play around!
Now your code is going to live in the code directory. This is a special folder I set up for you that is shared between your VM and your main computer, so you can use whatever text editor you like on your main operating system. I recommend Sublime Text 2 for Windows and OSX, and whatever you want for Linux since there are a ton of options there and I usually just use the terminal and nano in Linux. If you take the time to learn it, Sublime will save you a ton of time down the road.
Now, we’re ready to rock. Let’s open up our shiny new text editor, and make a new file called “hello-world.rb” The “rb” at the end (called a “file extension” if you haven’t seen it before) tells the computer this is a ruby file. This will become important down the line.
So we have our file, and we’re going to do the most cliched program in the world, something every single programmer in the history of software development did for their first program, called “Hello World”. Hello World simply prints the words “Hello World” to your terminal screen. In some languages it’s a ton of work, however, since we’re using ruby, it’s super easy to make:
puts "Hello World!"
In a nutshell this one single line of code tells the computer, “Hey, if you don’t mind, I need to see that thing in between the two double quotes right there on the screen”. Because this is so common every programming language has this function with varying degrees of complexity. You’ll be seeing it a lot since we’re a long way away from any kind of graphical user interface. Displaying things to the screen really helps later on when we make more complex programs at figuring out what’s going on.
Last thing we need to do is run our program and see it in action. So let’s log in to our Vagrant machine and run this code:
Mac and Linux: type in “vagrant ssh” and type in the password “vagrant” when prompted.
Windows: download and install PuTTY before running “vagrant ssh”
Once you are logged in you should see another shell. type in “ls” and you should see the “helloworld.rb” file in there. If so, we’re in the right place. Let’s run the file:
ruby helloworld.rb
You should see “Hello World” printed out to the screen. Congrats on writing your first program!
Small steps, to be sure, but in the next lesson we’ll start getting fancy!