Meeting-fols

All you need to know to start with Ruby

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:

  • a dynamic, object-oriented programming language
  • inspired by perl, influenced by lisp
  • functional, object-oriented and imperative
  • dynamically typed
  • mostly time interpreted (although there are some implementations of Ruby, which compiles a whole program to binary form)
  • a pink to blood-red colored gemstone ;)

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:

1.some_method

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 1 and +). 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.

So, + 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: {i_love: "Ruby"}

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:

  • www.ruby-doc.org - detailed documentation of all core Ruby classes and modules
  • CodeSchool - A couple of lessons on how to deal with Ruby
  • GitHub - This is a knowledge mine, and you’ll the find source codes for almost every gem pushed to rubygems
  • ruby-lang.org - a nice quickstart of Ruby in an official ruby site
  • TryRuby - the site mentioned in the article, introduction to Ruby inside web browser
  • Ruby Warrior - similar to tryruby, but for a little more advanced Ruby programmers, a nice game where you have to code your warriors’ steps in Ruby in order to win

Good luck and welcome to the Ruby world!

Other Ruby on Rails labeled posts from our blog.

On 10.02.2014 in dev and recruitment

Great news! We have 6 open positions waiting for you! Come and work with us!