All you need to know while getting started with Ruby on Rails
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 get started!
- 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 compile the 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.
Ruby is a fully object-oriented programming language, however it implements concepts from other languages, making it multi-paradigm.
Everything in Ruby is an object. And I do mean everything. Even a simple integer (1) is an instance of a class called Integer. Because it is an instance of the class, it is possible to call methods on it in the usual way, and for example add new methods to it.
Ruby is also general-purpose: It can be used to create simple system scripts, GUI applications, web applications, and even games. But Ruby is mostly known for Ruby on Rails, a server-side web application framework, which allows one to quickly create scalable web applications.
Every modern programming language has a way to track and install dependencies required for your application. For Ruby, it is Bundler - it is used almost in every Ruby application and was actually added as a default gem in Ruby version 2.6.
What is a gem? To put it simply - it’s third-party code you download and use for your project. For example: If you want to build a web application which requires an authentication solution, instead of writing it from scratch on your own, you can reuse an open source solution, such as Devise or Rodauth.
There is a very nice list of the most used and most common gems on the “awesome Ruby” list, available here or here. By the way - usually every programming language has an “awesome” list: If you want to see the list of libraries to use in Python, just google “awesome Python” :)
The majority of the available libraries are hosted on RubyGems, from where you will usually download gems automatically when using Bundler. You can also create your own gem and host it for free there for others to use.
When you are learning Ruby, I would suggest first trying to create a solution by yourself, to learn and understand how things could work, and then switch over to an existing one, for a stable and battle-tested solution.
There are tons of useful gems from Ruby on Rails itself, through helpers with application deployment, database integrations, XML and JSON parsers, payments, and search engines (Elasticsearch, Solr, Thinking Sphinx).
When I first tried Ruby, I bumped into this site: TryRuby. It’s a nice introduction to the 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.
If, when learning a language, you like to solve puzzles instead of reading through documentation and trying random stuff, I would recommend trying Exercism with the Ruby track. It contains 98 different tasks on different difficulty levels, so you can improve your skills in a scalable fashion.
How to download and install Ruby on your machine and start using it?
Most current Linux distros as well as macOS versions already have some default version of Ruby installed, so starting with Ruby should be easy, however the official site where you can download Ruby is https://www.ruby-lang.org/
There are a couple of categories of versions you can try and the important ones are:
- Stable releases
- The most recent stable releases of Ruby, which can be safely used in production, are still in development (meaning Ruby devs are constantly working on making them better and better!).
- As of today, these versions are: 3.0.x and 2.7.x.
- Preview releases
- Future stable releases, in preview state, where you can try them out, test and maybe report a bug or two. These are not ready for production.
- In security maintenance phase
- Versions not under active development, but still being maintained for security patches, although that support will end soon, so it’s time for an upgrade!
- The current security maintenance phase versions are: 2.6.x.
- Nightly snapshot
- Daily tarballs of whatever is in Ruby’s Git, made nightly. This may contain bugs or other issues, use at your own risk!
To keep your environment clean and be sure that you are always using the right version for your Ruby project, it’s always a good idea to use some kind of versioning software. The Ruby community created a couple, but I will focus here on RVM as it’s the most popular one.
On RVM’s main page there are a couple of steps with information on how to install it. It’s very simple! Head on to rvm.io and follow the “install RVM” steps. Now, you can install a specific version of Ruby. For example, write rvm install 2.7 in your terminal, 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.
2.7.2 :001 > puts "yay, it works!"
yay, it works!
2.7.2 :002 > RUBY_VERSION
2.7.2 :003 >
Using IRB is a great way to slowly try out some simple gems or small chunks of Ruby code.
Very recently, in December 2020, Ruby version 3.0 was released, which introduced some very anticipated features. These include:
- Pattern matching;
- “True” concurrency without GIL (using Ractors);
- Significant performance improvements (comparing to Ruby 2.0);
- Built-in ability to define the structure of a Ruby program, making it (almost) strictly-typed, using RBS.
This shows that Ruby is still growing and new things, as well as performance improvements and security fixes, are added to the language constantly.
Alright, now we have rvm, gems, irb, and some basics with Ruby! Here are a couple more links for you to start 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 find source codes for almost every gem pushed to rubygems here.
- ruby-lang.org - A nice quickstart for Ruby on the official Ruby site, an interactive tutorial where you will learn some basics.
- TryRuby - The site mentioned in the article, introduction to Ruby inside the web browser.
- Ruby Warrior - Similar to TryRuby, but for little more advanced Ruby programmers - a nice game where you have to code your warriors’ steps in Ruby in order to win.
- Exercism - Practice your programming language skills, contains a very large number of languages, Ruby included.
Good luck and welcome to the Ruby world!
UPDATE: You can find even more Ruby resources at Top Free Online Ruby Resources.