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 (of class Integer)! And I can call methods on it. For example:
1.some_method
All operators, like +, - etc. are instance methods of the Integer class. I can call them in two ways:
2.4.9 :001 > 1 + 2
=> 3
2.4.9 :002 > 1.+(2)
=> 3
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 because Ruby lets us modify existing objects, then we should be able to modify the behavior of + method, right? Well yes! We can do it! For instance:
class Integer
def +(number)
self * number
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. There are a lot of community-created libraries in Ruby that one may use in their project (as an example, rails is one of the gems). 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, 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 I bumped into this site: TryRuby. It’s a nice introduction to Ruby language. It consists of a 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.
The official site where you can download Ruby https://www.ruby-lang.org/
There are a couple of categories of versions you can try, and important ones are:
Usually every modern OS already has a preinstalled version of some version of Ruby (it depends on the OS, but really it should mostly be one of the stable releases).
To keep your environment clean and be sure that you are always using right version for your Ruby project, it’s always a good idea to use some kind of versioning software. Ruby community created couple ones, but I will focus here on RVM as it’s the most popular one.
On RVM’s main page there are couple steps with information how to install it. It’s very simple! Head on to rvm.io and install RVM. Now, you can install specific version of ruby. Ex write in your terminal rvm install 2.6.5, wait a couple of minutes 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.
{13:09}[2.6.5]~ ➭ irb
2.6.5 :001 > puts "yay, it works!"
yay, it works!
=> nil
2.6.5 :002 > RUBY_VERSION
=> "2.6.5"
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 Integer 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