Ruby is a one of the most popular languages used on the web. Today, we’re starting a new screencast series here on Nettuts+ that will introduce you to Ruby, as well as the great frameworks and tools that go along with Ruby development. Today, we’ll look at why you might want to use Ruby, as well as how to install it on your PC or Mac.
Watch the Screecast
Before we get started, let’s look at why you might want to learn Ruby. There’s a pretty good chance you already use PHP, or one of the other popular server-side languages. Is it worth it to pick up some Ruby?
Here are some reasons to use Ruby that I find compelling:
Ruby is flexible
As you learn Ruby, you’ll find that there are very often several ways to code the same bit of functionality. This means that you as the developer get to choose what is the most expressive way to explain what your doing. Many Ruby developers claim that Ruby allows them to write their code very close to the way they would speak it. That’s because of this flexibility.
Ruby is easy to learn
Ruby has a very low barrier to entry; within a few screencasts, you’ll find that you’ll be comfortable with the syntax and ideas. You’ll be coding in Ruby very soon, and it will only get better as you learn more!
Ruby has many great frameworks
The obvious one here in Ruby on Rails. However, there are a lot of other great Ruby frameworks, both for the web and not, that we’ll be taking a look at during this series.
Ruby appears simple on the surface, but is complex underneath
Finally, I like Ruby because it often appears simple on the surface. However, that simplicity masks an enormous amount of complexity. For example, some functionality that I’d have to write myself in another language is already built into Ruby, allowing me to write a single terse line of code.
Hopefully you’re convinced that learning Ruby is a good investment. One more thing: Don’t go into this thinking that you are learning Ruby to displace your current server-side language. While you could do that, you’ll find uses for Ruby apart from the web. For example, I’ve written a few scripts that just automate some of my repetitive tasks, such as dealing with huge numbers of files. Ruby isn’t just a web language.
Installing Ruby on Windows
Installing Ruby on Windows is pretty simple. Head over the Ruby Website and click “Download Ruby” on the right. When you scroll down to the ‘Ruby on Windows’ section, you’ll see that the easiest way to do it is by using the RubyInstaller. Right now, we’ll choose the latest version of Ruby, which is Ruby 1.9.2p0. Download it and install it, just like any other Windows program.
Once you’ve got it installed, it’s pretty simple to make sure everything worked. Just open a command prompt and type this:
$ ruby --version ruby 1.9.2p0 (2010-08-18) [i386-mingww32]
It’s that easy!
Installing Ruby on Mac
Installing Ruby on a Mac is a bit different; because OS X is based on Unix, using an installer (a la Windows) isn’t the way it’s done. First off, Ruby actually comes installed on OS X. However, this is Ruby 1.8; it’s perfectly fine if you use this; just know that there may be a few differenced between that and the Ruby 1.9 that we’ll be using. Don’t worry: for most of what we’ll be doing, this shouldn’t be an issue.
If you want to move to Ruby 1.9, you can do it two ways: first, if you use a package system like MacPorts or Fink (or maybe even Homebrew), you should be able to install it though that. If you’re feeling adventurous, you can install it from source code. If you decide to do this, be sure to follow the detailed articles available on Dan Bejamin’s site, Hivelogic. Here are links to the instructions: choose the right one for your version of OS X!
A note about his instructions: you’ll want to replace the URLs for the ruby source archive with the URL for the latest versions (available from the Ruby download page). Also, you don’t need to install anything other than Ruby 1.9. If you’ve heard something about Ruby, you might think you’d have to install the RubyGems library. RubyGems allows you to easily download or share little (or big!) bundles of code. This used to be a separate download, but it’s built into Ruby as of v. 1.9, so this is unnecessary.
NOTE: Although I didn’t mention this in the screencast, you might also want to look at Ruby Version Manager (RVM) for installing Ruby. I haven’t used this before, but I’ve heard reputable Ruby devs say good things about it.
Again, to make sure everything is good, run that some command in the terminal:
Now that we have Ruby installed, let’s look at one of the main tools we’ll be using for this: IRB. IRB stands for “Interactive Ruby Shell. This is like a command line for Ruby. You can type one line (or a few lines) or Ruby at a time, and you’ll see the returned value of that line of code being evaluated. Try some of these lines (the dollar signs ($) represent the IRB prompt):
$ 1 + 2 => 3 $ print("Hello World") Hello World=> nil $ puts "Hi there" Hi there => nil
As you can see, after you write a line of code (and hit enter), you’ll see the evaluation of the line, right after an “arrow.”
In the above example,
puts (think, put string) are function calls. Notice that in one, I’ve wrapped the parameter in parenthesis and the other I have not. Very often in Ruby, parentheses in a function call are optional. I could have called
puts with them. It’s up to your sense of style! Also, notice that both functions print the requested text to the console, but that’s not their return value: both return
nil, which is Ruby’s “nothing” value.
As a conclusion to today’s lesson, let’s look at writing a function. You can write a function in IRB: it’s smart enough to realize that it needs more before it can evaluate the function, and won’t return a value after each line of code:
$ def greet $ return "Hello there" $ end => nil $ def greet2 $ "no return necessary" $ end => nil $ greet => "Hello there" $ greet2 => "no return necessary"
This demonstrated the next Ruby syntax principle: while you can use
return at the end of a function if you want, it’s not necessary. Ruby functions will automatically return the evaluation of the last line of the function. Of course, you’ll use return when writing more complex functions, like ones will multiple possible return values.
In this tutorial, the first in our Ruby for Newbies series, we’ve looked at getting Ruby installed on your computer and looked at IRB, a very important tool that we’ll be using quite a bit as we explore Ruby. Thanks for reading, and let me know what you want to see from this series in the comments!