Photo by Financial Times
So you’ve decided to learn Ruby? Great to hear! It’s always a pleasure to introduce another person into the Ruby community.
Let’s start with some basics. Ruby is:
First and foremost, Ruby has beautiful syntax. Reading Ruby code is like reading a book, very simple and intuitive. Of course, it can be very complex depending on how we use it, for example, when we start to use meta-programming (dynamically adding methods, modifying classes, existing objects, etc. Yeah, Ruby can do that!).
Everything in Ruby is an object. And I do mean everything. For example, even a simple integer is an object! And I can call methods on it. For example:
All operators, like
- etc. are instance methods of the
Fixnum class. I can call them in two ways:
2.0.0-p353 :007 > 1 + 2 => 3 2.0.0-p353 :008 > 1.+ 2 => 3
Note: parentheses in Ruby are not always required.
In my first example, I used a simple 1 + 2 format, just like every language adds 1 to 2. In the second, I did the very same thing, but I explicitly called the
+ method (note the dot between
+). Well, actually, I also called the
+ method in the first example, but I didn’t have to use a dot, because Ruby interpretation allows us to call operators methods without it, to make your life easier.
+ is a method and Ruby lets us modify existing objects, we should be able to modify the
+ method, right? Well yes! We can do it! For instance:
class Fixnum def +(num) self * num end end
Note: In Ruby, you don’t have to call return statements, it will return the value from the last line of operation in code.
In the above example, I can now do: 2 + 3 and get the result 6. I have modified the + method to act like a multiplication instead of addition. You might ask now: Woah, so if I can do that, people must troll A LOT in those gems, right? Evilly modify basic methods and make a trash out of code? Well yes, it’s possible, but it doesn’t happen :-). What’s the fun in doing something bad, if it’s that easy!
When I first tried Ruby (it was couple years ago) I bumped into this site: TryRuby. It’s a nice introduction to Ruby language. It consists of couple of small tasks which you have to actually code in Ruby to pass. In 15 minutes of using this site, I think you’ll fall in love with the language.
After you have leveled up your Ruby skill, you want to download Ruby on your machine and start using it right away. There are a couple of methods for doing this. Usually every modern OS already has a preinstalled version of Ruby (it depends on the OS, often it’s 1.8 - an outdated, slow and non-maintained version). For the beginner, it doesn’t really matter what version of Ruby are you using. However, if you are for searching Google for some Ruby answers, it’s a good idea to assume that people are using at least version 1.9. This is a good version to use so that you don’t bump into any errors, like syntax errors with the new ruby hash introduced in version 1.9.
For example, this piece of code:
Will be a syntax error in 1.8, while in 1.9 and after, it will work fine.
Ok, to install and manage rubies in your system the easiest and most common tool for this is rvm.io. It’s simply a matter of calling:
curl -sSL https://get.rvm.io | bash
In your terminal, and you’ll have rvm set up. Now a command rvm install RUBY_VERSION will install it, and you are ready to go!
Each Ruby version contains an Interactive Ruby Shell (irb) in which you can safely test your ruby code. You can run it by simply calling irb in your terminal.
=> ~<ruby-2.0.0-p353> [11:55:28]$ irb 2.0.0-p353 :001 > puts "yay, it works!" yay, it works! => nil 2.0.0-p353 :002 > RUBY_VERSION => "2.0.0"
I think using IRB is a great way to slowly try out some simple gems, or small chunks of Ruby code, or what I did with the Fixnum class :-).
Now - what are those gems I keep talking about? It’s nothing more than encapsulated pieces of codes that are commonly known as libraries. You’ll use gems in your application. It’s usually a single file (with a .gem extension) which, if installed in your Ruby environment, will unpack itself and copy the contents of the .gem into the proper directories (which you don’t really care about - think of it as a .deb). You install it, use it, and forget about it. Gems are a core functionality in the Ruby world.
You can find the repository of existing gems at RubyGems, and eventually you will most likely use this site to download gems, even though you may not be fully aware of it (simple gem install commands fetch gems from this site!). You will use gems a lot, for example Rails is nothing but a gem, which contains some pure Ruby code, expanding it significantly so that you can use Ruby to build a web application.
There are tons of useful gems from Rails itself, through helpers with application deployment, database integrations, xml / json parsers, payments, and search engines (elasticsearch, solr, thinking sphinx) integration. And they are all FREE! All gems that are sent to rubygems are under open source license, so you can safely fork them, modify them, and use them in commercial use!
Alright, now we have rvm, gems, irb, and some basics with Ruby! Here are a couple more links for you to start with your Ruby adventure:
Good luck and welcome to the Ruby world!
UPDATE: You can find even more Ruby resources at Top Free Online Ruby Resources
Great news! We have 10 open positions waiting for you! Come and work with us!