Ruby for Newbies: Installing Ruby and Getting Started
basixvideos

Ruby for Newbies: Installing Ruby and Getting Started

Tutorial Details
  • Topic: Ruby
  • Difficulty: Easy
  • Estimated Completion Time: 30 minutes
Download Source Files
This entry is part 1 of 13 in the Ruby for Newbies Session
Next »

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


Why Ruby?

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: ruby --version.


Meeting IRB

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, print and 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 print without them or 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.


Summary

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!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.nexuswebsol.com muhammad saqib

    great….love to see this series

  • http://twitter.com/immysl Immysl

    This is really good for us who have no idea about Ruby. But why are Linux users being discriminated? No instructions on how to install Ruby on Linux there. :(

    • http://tinyurl.com/jonatanfroes2 Jônatan Fróes

      Its so easy that we don’t need totorial:
      sudo gem install rails –include-dependencies

    • http://www.maxluzuriaga.com Max Luzuriaga

      As far as I know, installing Ruby on linux is either the same as or similar to installing it on the Mac. If not: let me Google that for you.

    • Nick

      Here

      sudo apt-get install ruby && sudo apt-get install rubygems && sudo gem install rails -y

      (of course you can remove the &&’s and do it step by step)

      replace apt-get with your package manager (yum, emerge etc)
      -y means include dependencies but it’s default in newer ruby versions now

    • http://twitter.com/immysl Immysl

      Thanks everyone. But what bugged me was the sidelining of Linux you know. :-/ Anyway, that isn’t an excuse for me to not google for it. ;)

      • http://andrewburgess.ca Andrew Burgess
        Author

        Sidelining Linux wasn’t my intention; I just thought that most people using Linux wouldn’t need this kind of hand-holding.

  • http://www.agenciafly.com.br Jônatan Fróes

    I’ve been waiting a long time for this serie …
    thanks!

  • Phil

    This is great I’m really looking forward to the rest of this series.
    :)

  • Junxuan

    I’m looking forward to this series :)

  • http://mkaito.github.com Michishige Kaito

    I’m glad to see this series here on tuts+, since we mostly only see PHP articles. The Ruby comunity is a lot bigger than most people think, and job oportunities for Ruby developers are always spawning. There are many reasons to like one tool over another, and everyone has to find their own. My own would be:

    TDD/BDD is a highly respected part of the comunity, and the amount and quality of the available tools is just astounding.

    Rubygems, while having a bunch of flaws, is one of the best topic specific package managers I’ve worked with. And with Gem Cutter, anyone can publish their stuff very easily. And what do you know, Gem Cutter wasn’t done by anyone in the Ruby core, but by a Ruby development company. This probably shows a lot about the quality of the comunity.

    The flexible syntax, especially the fact you don’t need to use parentheses most of the time, make for very readable and almost beautiful code, as well as allowing for many different coding styles. Your code, your way.

    Open Classes allow to modify existing classes on the fly, including built in classes. The heavy use of OOP of Ruby makes this a very strong point. You can add or modify methods to anything, anywhere.

    Mixins are probably one of the most useful design patterns I’ve come across. Once you grasp how they work, you’ll want to use them everywhere.

    Ruby is actually a very good language for off-web tasks, such as automation scripts, or even writing Vim plugins (if you compiled it with Ruby support). Once you get into Ruby, you’ll want Ruby everywhere, and Ruby happily comes along.

  • http://imkwaa.com Olle

    Incredibly clean and nice tutorial. I’m loving it. Will this be weekly?

  • http://www.darrnick.com Darren Nickerson

    Good introduction on ruby, could have used this a couple of days ago when I started to learn ruby. Better late than never though. Looking forward to the next screencasts.

  • http://alexpearce.me Alex

    Ruby Version Manager (RVM) is a much saner way of updating the version of Ruby on an OS X machine, as opposed to MacPorts or (eek!) building from source. This screencast by Ryan Bates explains it very well and succinctly, before moving on to installing a (now defunct) Rails 3 beta which can be skipped.

  • http://www.palavalli.com Nagarjun Palavalli

    Would learning Ruby be of any use if you know / use PHP? Is it better than PHP or would it go well with PHP as an add-on language to create other amazing stuff? I’d like to know before I set out to learn another programming language.

    • http://www.johnsthomas.com john

      The more programming languages you learn, the better programmer you become. I learned Ruby after PHP, and found that it changed a lot of the ways in which I think about solving problem. I still code in PHP at my job, but I use a ton of the logic I learned from the ruby community.

  • Mihai

    What web server will i neet to run ruby scripts as web page ?

    • http://twitter.com/scriptin Dmitry Scriptin

      Any, but you have to attach a ruby interpreter somehow (apache module or CGI).

    • http://andrewburgess.ca Andrew Burgess
      Author

      Don’t worry, we’ll be looking at all of that! There are several options, though, depending on what framework you’re using. Rails comes with it’s own server; when we look at Sinatra, we’ll use Shotgun.

  • Ryan A

    Another way to install ruby (and probably considered the current way to do it – it changes so often), is RVM:

    http://rvm.beginrescueend.com/

    It’s literally as easy as running that single install command, then typing ‘rvm install 1.9.2′ and you have it up and running, and the best part is you can install different versions side by side (especially useful if you’re still maintaining older projects).

  • http://andrewburgess.ca Andrew Burgess
    Author

    Hey Everyone!

    Thanks for the comments; glad to see people are excited about this series! Just wanted to say that I’ve had a chance to use RVM to install Ruby. If you’re on OS X, you should definitely use it. I was having some problems working with gems when upgrading to 1.9.2, but using RVM solved that!

    And, will it be weekly? Well, hopefully it will be most of the time.

    • http://www.nexuswebsol.com muhammad saqib

      hey andrew..in this series you will go for ROR or this series will just cover Ruby programming???

  • SwissTony

    Hey Andrew,

    I’m looking forward this series also. I’ve done some tinkering with Ruby and it looks interesting, but i never got passed the using the Ruby shell stage.

    I am very keen to learn how it all ends up in the browser – I hope that will be covered!

    Thanks

  • http://www.arisb.gr Aris

    Hello! Great job on this new tutorial series!

    I would like to ask you some questions, is Ruby really that powerful? Can it compete with the most powerful platforms ? Can an entire complex website rely on Ruby?

    Again, bravo Andrew!
    Greetings from beautiful Greece! Aris.

    • http://andrewburgess.ca Andrew Burgess
      Author

      Can it compete?!? ;) It sure can; I’d say that Ruby *is* one of the most powerful platforms. And we’ll be building websites with Ruby frameworks before long. If you’re looking for an example of a powerful Ruby site (using Rails, I believe), check out the Envato Marketplaces. They’re all running on Ruby.

  • eric

    Looking forward to more.

    Happy Thanksgiving Andrew!

  • http://tiny.cc/mialazar Mia Lazar

    OK, but I like much more when is support on web hosting server, sometimes I can do on my own computer many cool things but on server nothing on that.

  • http://www.demogeek.com DemoGeek

    Being programming for > 12 years I can certainly vouch that Ruby is more elegant than others. And I love to do all my work on Rails as well but deployment was a big hold off for me. I really like the simple deployment process of PHP/CI than to go through a whole bunch of steps in Rails (Capistrano) to get this rudimentary thing done. I wish there is a simple way for me to get hooked with Rails. Any pointers?

  • David

    I think you found your next big series of tutorials.

  • http://twitter.com/mixn Milos Sutanovac

    This wasnt as hard to follow as I expected, but I was still hoping to see how to get this rolling on a Mac. Anyway, the link you posted helped me out and I was able to follow everything very well.

    Some things that appear very easy arent as easy as you might think when youre an absolute terminal beginner and working outside of an editor for the very first time.

    Another thing Ive realized, maybe thats just me, Im kind of scared while using the Terminal. Especially when I was installing Ruby (and Rails).. so much was happening and I didnt know anything about it. Weird feeling.

    Anyway… hoping to see another episode soon, thank you so far, Andrew.

  • Chris Simpson

    great, i’ll be following this series.

  • http://joeybryan.com Joey

    I’m so excited to start using Ruby. Thanks for this tutorial… I’ve been waiting for a while.

  • Nicolas

    I been interested in learning Ruby for quite some time, so I’m really looking forward to this series.

    Thanks for the help

  • http://cameron-malek.com Cameron Malek

    I just started getting into Rails and would love to see more out of this series! I was beyond all that introductory stuff except for one big game-changer: irb! I had no idea I could mess around with ruby like that!

  • http://www.steenvoorde-ict.nl Webdesign Utrecht

    Great clean introduction to Ruby! :D

  • http://newarts.at Drazen Mokic

    Who has problems using MySQL with Rails should try this fix:

    sudo install_name_tool -change /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib /usr/local/mysql/lib/libmysqlclient.15.dylib /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle

  • http://garethjoyce.co.uk Gareth Joyce

    Good start!

    It says “Watch the Screecast” above the vid.

  • Rene

    I’m a bit confused. So I followed the Snow Leopard link to Hivelogic’s instructions.

    I’ve never done anything with Ruby, yet I somehow already had Ruby 1.8.7 and Gems 1.3.7 installed? Do I still need to follow those instructions for any reason?

    I assume this applies to anyone else that has an up-to-date Mac?

  • http://www.skmvasu.com Vasudevan

    Thanks for the great intro. Would appreciate if you could throw more light on the architecture of ruby and some IDE’s.

  • daredanger

    Very low volume. :(

  • http://www.soltani.dk Matt Soltani

    Great tutorial, love your approach. This will help me get me started right away. Thanks Andrew.

  • http://www.webfresser.com Joshua Van

    Totally awesome tutorial. Thanks a lot for it, pal.

  • http://iamsmarts.com Rudy

    Great! exactly what i was looking for!

  • http://www.wavepointmedia.com Steve

    Broken link in your index or articles:
    Operators and Methods

    http://net.tutsplus.com/tutorials/ruby/ruby-for-newbi%E2%80%A6-their-methods/

  • Salman

    Thanks for the great tutorial,

    I tried to test the codes in windows 7 command prompt (cmd) but it wasn’t going the same way as you mentioned here. Even the IRB wasn’t performing as what you’ve said. Am I doing anything wrong here? I installed Ruby (rubyinstaller-1.9.2-p136.exe) but except the version can’t run any other code on it.

    Cheers!

    • http://www.red-root.com Luke

      I’m having the same issue, cant seem to get IRB running on Windows 7, is the prompt different?

    • http://www.red-root.com Luke

      Found a solution. On Windows 7 if you go to All Programs -> Ruby 1.9.2 there is a application called Interactive Ruby, this is the IRB :)

  • http://www.globalkaynak.com Erbil

    Hi,I have used the python for a short time.But now,I’m hearing that Ruby (with rails frameworks) is great language for web.I have watched the video,but I can’t see any difference between ruby and python.
    is there any good reason to stay with ruby?

  • Chinnappa

    I found this helpful.

  • http://www.facebook.com/pages/Bhagu-Art/105358672430 Vipul

    Hi, Andrew! Great Tut, and great help for beginners like me… lots of well wishes… :D

  • Ammar

    Hello,

    I’ve just upgraded to Lion (which ships by default with ruby 1.8.7), is RVM the best method to use to run ruby 1.9.2 on Lion?

  • http://catinacrate.com Joseph Curcuru

    Thanks for this. Very helpful.

  • Dee

    For those who are TOTALLY new to programming, it was a little confusing that the author was calling a method a function. I’m sure “verbs” are called functions in other programming languages but it’s called methods in Ruby. Thanks for posting.

  • Farzin

    thanx guys. I really appreciate what you do in tutsplus.

  • http://twitter.com/transcendancing Transcendancing

    Just a note: your examples say to type ‘$’ with the commands, and they don’t work if you do. It took me ages to work out that it wasn’t necessary and came from unix, and that it’s the > that is the equivalent thing in windows, which is already there in the command prompt.