Getting Started in Ruby (The Advice I Wish I was Given)
This post is geared toward a close friend of mine who is on the fence about programming but could easily be applied by anybody trying to get their start in Ruby. Like most people, this individual could do great things if he’d just get started. Unfortunately, like most people, he is also notoriously good at procrastination when he doesn’t have a clear objective or path, such as when you first start learning to code and everything seems like magic you’ll never understand. I’m aiming to take down some of those barriers to entry.
This is also serving as my own bit of procrastination before I finish up the series of posts on Eloquent Ruby.
Ruby is Object Oriented
The basic goal behind Object Oriented Programming (OOP) is to create a representation of real world objects or concepts in code. If we look at a door, we know the door has some very basic attributes and functions. The door has a height, width, color. The door can open and close. Since all doors have and do these things, we can try to represent that in code, wrapping that code in something called a class.
Classes are like blueprints. Using our door example again, we know there are many instances of doors out there in the world. How do we make them all? Using our class, we can pump out as many instances of a door as we see fit.
Here’s an example. Don’t worry too much about syntax right now, just understand that we’re blueprinting a door. Anything behind a
# is considered a comment by Ruby and is passed over.
class Door # defining the door's functions def open # code that opens a door end def close # code that closes a door end end # At this point, Ruby only knows how to # build a door using this blueprint # There are no instances of a door out there yet # So let's create one: my_new_door = Door.new
And just like that you’ve created an ultra simple model of a real world door in code. Anytime you want to open your door, you use the code
my_new_door.open. Wanna close it?
my_new_door.close. Now imagine creating more conceptually interesting classes, like for Tweets, Relationships, or UserAccounts. You can start blueprinting and building some very useful stuff for people. And that’s our job as future devs, building things that help people.
Now for the tools.
Terminal + Bash
I know you’re a little familiar with Terminal already so that’s good. Terminal is your command-line interface. It provides you an environment for which to interact with Bash.
Bash stands for Bourne Again Shell. Without focusing too much on the first two words, that leaves us with the shell part. Shells provide us with command-line interpretation and therefore give us tools we need to interact with our computer through the command-line. Here are the ones you need to know right now.
cd some/directory→ Change directory into
cd ..→ Go backwards one directory
cd ~→ cd into your Home directory
mkdir some_directory→ Make a directory
rm something→ Remove
rm -r some_directory→ Delete a folder and all its contents.
sudo [command]→ Sometimes, your Mac is going to want permissions. This will prompt you for your system password.
Your computer comes preinstalled with Ruby. If you’re on Lion, it’s Ruby 1.8.7, which is fine for learning purposes but I think it’s also important to understand some of the more basic tools for Ruby programmers. That’s where RVM comes in. RVM stands for Ruby Version Manager and it’s just about the greatest thing for trying out different Ruby environments.
We’re going to install it using these commands in Terminal. Anywhere you see a
$, that’s your command-line prompt:
$ curl -L get.rvm.io | bash -s stable
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
Close Terminal and reopen it. Test to see if it worked by typing this:
`type rvm | head -1`
If you get
rvm is a function in return, it worked.
Now, let’s install the latest version of Ruby.
rvm install 1.9.3
And to make it the default:
rvm use 1.9.3 --default
You’re good to go. If you run into trouble somehow (because it happens) or just want to learn more, RVM’s website has some very helpful documentation.
Install TextMate (or any decent text editor out there)
Really this comes down to preference. And since you don’t have one right now, give TextMate a shot. In the end all you need is something that gives you monospaced text and some syntax highlighting. SublimeText is another good option I haven’t used but have heard good things about.
Your First Program
Alright, it’s time for your first program. Here we go.
mate helloworld.rb into your command-line.
2. Type out the following into
puts "Hello, World!"
3. Save then go back to your command-line and type the following:
And there ya go, your first program. It’s no Twitter, but you gotta crawl before you can walk.
How OOP Plays Out in Ruby
When we wrote out the first program, we used a predefined function
puts to print out a line of code in Terminal. By the way, from here on out, I’ll call functions methods. For our purposes, they’re more or less interchangable. Anyway, our
puts is cool but we want to model the world around us so we need to define our own methods.
In order to better organize and describe our models, we’ll keep everything in classes. Just like I was saying above, classes are blueprints for real world things or concepts.
We define our class using this syntax.
class NameOfClass # notice no underscores between words. New words have first letter # capitalized. This is good syntax for class names. # code end
We can create a new instance of a class by using this syntax:
new_instance = NameOfClass.new
This gives us a newly manufactured instance to play with. We can also give our class stuff to do:
class NameOfClass def our_method # variables and methods use underscores to separate words. No caps anywhere. # code end end
See how we create the code sandwiches?
def/end. This scopes our code. In other words, even if we have the same variable in two different classes, because they are scoped by the class they are in, they are not available to other classes (…yet. We can do a little work to make it so). In this context they’re called local variables. The same goes for methods. The variables inside a method are not the same as the variables in other methods, even if they have the same name or are in the same class. This keeps things sane for us. (Again, there is a way to make it happen but that’s slightly out of our scope right now. Pun INTENDED.)
helloworld.rb with this in mind.
# first we create a class class Michael # then a method def speak_up our_variable = "Hello World!" puts our_variable end # then another method def are_flatulent_sometimes our_variable = "Brrrrrrrip." puts our_variable end end
Class created. After the last end in your code, put the following on the next lines.
you = Michael.new # there are many michaels but only one you! you.speak_up you.are_flatulent_sometimes
Now run the code using
ruby helloworld.rb. See how our methods printed out different things even though they used the same variable name? This modularization of code is a useful facet of OOP.
Wanna just play with Ruby without having to use files? IRB (Interactive Ruby Shell) is basically a virtual playground for coding. Start it by typing
irb into the command-line. You can mess around with anything you aren’t sure about before you hardcode it into a file. As an example, you can copy and paste the above class into IRB and create as many new
Michael instances as you want.
It’s a lot of stuff to learn no doubt. But give it enough time. Stop playing WoW if you are again and really work at this. Either you truly want to learn like you keep telling me or you don’t. I have no doubt you’ll be successful in life even if you fail at this because you’re smart. But that’s the problem with people telling you you’re smart: when something doesn’t come easy to you… well, then it takes some sort of genius to do it. This is not the case here nor is it really the case for anything in life.
Start the Michael Hartl book. It will help you find a goal for all this. You know how to reach me if you need help.