Programming Tutorial: Intro to Ruby! Part 1

, , Leave a comment

I often find myself describing the joys of programming to my friends at school. I tell them about all of the possibilites, the creative liberty, the instant gratification one feels after running code without any errors or bugs. Everyone seems interested enough, but there is always one problem: Nobody knows where to start. I usually try to remedy this situation by showing them how to install Apple’s Xcode, or some other IDE (Integrated Development Environment); but by the time they are done downloading stuff, they are already bored. Instead, I could have just showed them how to run simple ruby code using the terminal app and a text editor, tools that come preinstalled on every mac.

What is Ruby? Ruby is a cross-platform, object oriented programming language that originated in Japan during the mid 1990s. It was first designed and developed by a man named Yukihiro Matsumoto, or “Matz” for short. It is said that the languages Perl, Smalltalk, Eiffel, Ada and Lisp most influenced the development of Ruby. Ruby focuses on simplicity and productivity, and has an easily readable syntax. This means that the code is both easy to read and understand. Alright, enough history. Let’s dive right in.

 

Getting Started

Formatting Note: I have made all of the code Bold. This means that everything I want you to enter into the terminal (or later on the text editor) will be in Bold. This will help you differentiate it from the rest of the text in the article.

Open up the terminal application on your mac (located within the Utilities folder inside your Applications folder). You should see either a white or black window with a string of text that looks something like this: “Reeds-MacBook-Air:~ reedrosenbluth$”. All this means is that I am using a computer called “Reeds Macbook Air”; I am in the home directory (which is represented by the ~ symbol), and I am logged in as reedrosenbluth. I will cover some of the basics on how to explore your mac only using the command line within terminal, but for now let’s continue with some Ruby.

To play around with Ruby, you must simply activate a program called Interactive Ruby, or IRB, within your terminal. Luckily, this comes preinstalled on every mac, so all you have to do is type irb and hit enter in the terminal window. You should see this “>> “.

Ok, so it’s open… Now what?

Well, let’s start with some basic math. Type 2 + 2, and then hit enter. The terminal will output the number 4. Cool right? Try 2*3, or (2+2)*3. You will see that Ruby obeys some of the basic math principles you learned in middle school, such as the order of operations. What about 3^2? Try it…. Uh Oh… That didn’t work did it. That’s because the ^ symbol does not raise numbers to a power in Ruby, it is actually a Bitwise operator (don’t worry about it for now). In order to raise a number to a power you must type something like this: 3**2, which will square the number 3.

 

Variables

While Ruby can help you with your math homework, the extent of it’s capabilities exceed basic algebra. One of the key aspects of programming computers is using variables to represent data. Using variables in Ruby is very simple. In many other languages you have to declare a variable before you can assign it, in order to specify the variables type. In Ruby, this is not necessary. All you have to do is assign the variable like so: type x = 10 into your terminal window and then press enter. You just assigned a local variable (x) the value 10. Now that you have done this, try entering this: x*3. You should get an output of 30. Try typing y = 8. Now enter (x+y)**2. This will add x (10) and y (8) and square the result.

Variables aren’t just for numbers. Type: name = “George”, and hit enter. You just assigned a variable (name) the string “George”. Now try typing: puts “Hi, my name is #{name}”, and hit enter. This code outputs a string (a sequence of characters) with an embedded variable (name), also, under the output you will see this: => nil. This means that the code returns nil, Ruby’s “absolutely-positively-nothing” value. Don’t worry about what that means for now.

Variables are a fundamental aspect of computer programming. Imagine writing a large program, with hundreds of lines of code, where in each line you perform a different mathematical operation on the number 30. All of a sudden you realize you were using the wrong number, you needed to use the number 40 instead of 30. If you had already written this program you would have to go through your code and replace every instance of the number 30 with 40. This would be a huge hassle If you instead assigned the variable x = 30 at the beginning of your program, and used the variable x throughout your program (instead of 30), you could easily change numbers by setting x equal to something else at the beginning of your program.

You are probably thinking that you can’t go back and edit your code within the terminal. You are right, the program we are running is an interactive version of ruby. The more common way of programming ruby is to program it in a text editor, and then run it using the command line (terminal) or a ruby IDE (Integrated Development Environment).

 

Getting started with a text editor

Most programmers write code in either a text editor or an IDE. For this tutorial, we will be using a text editor. Although you can write code in text editors like text edit or Microsoft Word that are already installed on your computer, I highly recommend downloading a text editor that is meant for programming. These editors will have necessary features such as Syntax Highlighting that your typical text editors don’t have. I personally use Sublime Text 2 for most of my coding needs besides Objective-C (iPhone and Mac Programming).

Here is their website http://www.sublimetext.com/.

There is a mac version available here: http://www.sublimetext.com/blog/articles/sublime-text-2-beta.

Another great, free text editor is Text Wrangler.

Open up your text editor of choice and create a new file. Save the file to your desktop as HelloWorld.rb (it is very important that this file name ends with .rb). Edit the file and type in this code: puts “Hello World”. Save the file. Now, go back to your terminal window, if you are still in IRB (you still see the >> prompt) press control D on your keyboard to exit (if this doesn’t work, just restart terminal). Now, type ls in terminal and hit enter. You will see a list of the directories in your home folder on your computer. Type cd desktop and then hit enter to navigate to your desktop directory within terminal. Now type ruby HelloWorld.rb to run the ruby program we just wrote. If everything worked, you should see the output Hello World. Congratulations, you just successfully ran a ruby program.

Although the Hello World program is not very interesting, there is a lot of cool stuff you can do with Ruby, and programming in general. Think about all of the applications you use on your computer on a daily basis. Your browser, your text editor, iChat. These all started out as lines and lines of code.

Alright, that’s it for part 1.

Part 2 will cover: getting and putting input, testing a condition, and defining methods.

 

Leave a Reply