EDITING BOARD
RO
EN
×
▼ BROWSE ISSUES ▼
Issue 37

A Short Handbook for the Unicorn from Ruby on Rails

Emil Luța
Developer @ Yonder



PROGRAMMING

Ruby on Rails starts slowly to come out from under the hipsters' influence area, as an increasing number of products are being developed using this framework. Adding this to your tools can surely be a plus for your career. Furthermore, surely you will be called a "unicorn" if you are programming in Ruby on Rails - everyone has heard of such developers, but no one has actually seen one in real life.

However, before delving any further into the matter, let's see what you might need for this "ruby on rails".

We notice a modest start in 1995 as a very obscure programming language and we may infer that its popularity comes from the Rails framework, which has created an enthusiastic community. Rails was designed and created by David Heinemeier Hansson in the summer of 2004. For more specific details about Ruby>)(1) and Rails(2) please see the references at the end of the article.

An analysis of the diagram above allows us to infer a number of interesting things: Ruby is much stronger than PHP and Javascript and is very close to C#.

What impresses is the fact that Ruby is such a viable solution in comparison to other popular options used at this time in the development environment.

You can find below a mini-collection of websites which, I believe, reflects the potential of the Rails framework for web-based applications.

Top 5 websites built under RoR

  1. Twitter - in its first years of existence, Twitter was based solely on RoR; later on the decision was made to move it in a Java environment.

  2. Basecamp - the project on which David refined Rails.

  3. Yellow Pages - the famous book with telephone numbers stepped into the virtual world under the name of YellowPages.com.

  4. Github - is any comment necessary?

  5. urban dictionary - a crowdsourcing type of project for an online slang dictionary.

And now, let us delve into the particularities of Rails which turn it into such a publicized avant-garde.

The Benefits of Ruby on Rails

1. Convention over Configuration

I cannot stress enough how this small functionality make Rails not only unique, but also formidable. Having this tool available, a developer can concentrate on code in a pragmatic manner, instead of using their energy and attention on the configuration files. This aspect can be best observed in the Model-View-Controller type of architecture that the language enforces. The manner in which it manages this is interesting enough to raise smiles on any developer's face.

Thus, instead of having to configure which model (atomic unit for storage in the database) goes to which table from your database, Rails offers you a very simple rule: let's suppose you have a model called User; in this case you will know for sure that it is going to be associated with a table named Users (the plural of User). Even more, the logic responsible for the code handling the model is to be found in the controller named UserController, which will be mapped for a set of pre-defined routes by the resource named users (endpoints of the RESTful type are offered - index/ show/ new/ create/ edit/ update/ delete) which will correspond to the methods of the same name defined in the controller as access points. Then, of course, the views rendered by these methods can be found in the Views folder, in the Users subfolder, each having the same .html name as the endpoint.

Now, what would it be like if all these were to be generated with a single command?

Well, the scaffolding allows for this possibility, having available an application which runs in only a few seconds (with Rails already installed). I don't know what your opinion is, but I find it useful and quick! It is noteworthy the fact that the conventions can also be changed if this is desired, but Rails emphasizes the idea of using them as they are provided.

Another reason for which you will love this practice is the ease of the users to go through any project. If you have once learned these conventions and you have to move to another Rails projects, there are very high chances that you will understand the logic behind the actions from a simple UML. Furthermore, you will be able to manipulate the code without having to receive information from the previous developers. Now I'll ask you again: isn't this great? Don't worry; there are also other strong points!

2. The Community

From this point of view, we can say that you are really grounded. There is a huge library of the open source type available for Ruby on Rails. Even more, the majority of the sources are very well documented (4) (I often think of how they managed to bribe the programmers so that they would make the documentation of such high quality!) and exemplified (5) everywhere.

Anytime you need something, there is a gem (I'll come back to this) especially constructed which solves your problem in the "Rails style".

In what the gems are concerned, you can see them as similar to the libraries or plug-in-s from other programming languages. In order to use the gems through the application, there is a file called gemfile which allows you to add or delete as you wish any library through url. In order to make sure that your machine or other projects, each with their own different gems, each having a different version, are not polluted, there are several approaches. I suggest confidently using the rvm-(6) (ruby-versioning-manager).

The last bonus of the community is the fact that it is pro-active. Generally, communities linger, grown old or are even a hindrance in the projects of the boom-type. But this is not the case. Even more, one can say that it is even more enthusiastic than when it all started!

As one last aspect - if you even need to solve a certain problem and you don't know how, railscasts(7) will guide /inspire you most of the time in finding a solution.

3. It Makes You Happy!

No, it is not a bad joke. Ruby's aim, as underlined by its creator, was to make the programmers happy. This is actually the thing that drew David from the very beginning. After literally falling in love with the syntax, he decided that Ruby is the basis on which he will start his framework. And, in order to carry on with the tradition, he optimized it even more in order to bring programmers as much happiness and as few nuisances as possible.

And still you can ask yourself: how exactly does this make me happy? Well, alongside the fact that you can write code almost in pure English, you also have the benefit of getting rid of the most boring bits of the web development (avoiding the configuration files!), all these being accessible to you in the quickest manner. From my point of view, this would make the most hardcore programmer wag their tail.

4. You Don't Have to Have a Duplicated Code

This philosophy is strongly embedded in the Rails' DNA. According to the methodology in which the code is divided in models, controllers and helpers, you will never need to duplicate the code. Even more, the logic is already extracted in the gems by the community (see how they are interacting?!) and you can use them by simply inserting them in your gemfile. Thus, if you need an admin panel, you can always import activeadmin which is both minimalistic and stylish, but on top of all this it is also configurable. If you need authentication solutions, devise will do it for you. Then, probably you will need authorization (taking into account the fact that you have users, you will have types of users, and in the end this is the aim, they authenticate in order to see different content), without any problem, as cancan solves this for you. And the list (8) goes on…

5. Expressiveness

The convention is useful because it relies on the configuration files and the relationships that you are memorizing. This is the number 1 benefit. Then there is a second one, namely Ruby's expressiveness, which, I say it again, is the main reason for which David chose Ruby over any other language. You may ask: how is this useful? Well, in several ways.

For starters, when you are writing code you are feeling well. Not only you don't have to set everything at every step through the application, but also you can easily deduce functions that are already implemented. For example, when I started to learn Rails, at a certain point, I needed to know whether in a list of strings, include includes a particular string that I had calculated. And, of course, there are always the two options: you iterate through it, in the classical manner and you check at every step, or you search it on Google. So, here I am, searching on Google "find string in strings array Rails" - and this was the first link(9) that I clicked on. I will allow you to find out what surprised me when I opened it.

And this has happened several times when coding. I even reached the point when I was guessing which verb or which combination of verb and adverb will give me the right answer, and, in case it did not work, I searched on Google (sort_by, .present? .empty?).

So maybe you are not impressed. So I will share another small secret with you. Rails resembles English so much because it uses an internal DSL(10). How is this good for you? Well, allow me to write a code snippet for you; then, I will ask you to read it.

class User < ActiveRecord::Base  
 devise :confirmable, :registerable  
 validates_presence_of :name, :day_of_birth, :email    
 before_save :compute_age    
 has_many :books    
 has_attached_file :avatar, :styles => {:small => "240x240>"}

Are you not convinced yet? Let's try it in a different manner. Show this snippet to one of your friends and ask them which is the easiest to read:

Ruby
return Fridge.get_beer_if_available
PHP
$result = $fridge->getBeerIfAvailable($beers);
Java
return Fridge.getBeerIfAvailable()

The main advantage is that you can express your thoughts easily using English, without needing a supplementary level of abstractness. Furthermore, the code is simple, so that you can read it as English.

Conclusions & Resolutions

Ok. So, it seems we reached the end of the article. I am certain I did not persuade you to ask your boss if he wouldn't want to steer the profile of the company towards Rails. But… from now on, I think there is no reason why you should not have items on the list of good ideas to be implemented. You have the tool to make quick changes to an application and to fuel your curiosity in seeing how it could work. So, why not do it? As a conclusion, I want to underline the fact that RoR will make both you and the owners of your product happy.

P.S. Here are some resources that could be useful

Getting started with Rails tutorial.(11)

Deployment options.(12)

Official documentation.(13)

Official tutorial.(14)

References:

  1. https://en.wikipedia.org/wiki/Ruby_(programming_language)
  2. https://en.wikipedia.org/wiki/Ruby_on_Rails
  3. http://www.unlimitednovelty.com/2009/04/twitter-blaming-ruby-for-their-mistakes.html
  4. http://api.rubyonrails.org/
  5. http://guides.rubyonrails.org/getting_started.html
  6. https://rvm.io/
  7. http://railscasts.com/
  8. https://rubygems.org/
  9. http://stackoverflow.com/questions/1986386/check-if-a-value-exists-in-an-array-in-ruby
  10. http://en.wikipedia.org/wiki/Domain-specific_language
  11. http://12devs.co.uk/articles/writing-a-web-application-with-ruby-on-rails/
  12. https://www.heroku.com/
  13. http://api.rubyonrails.org/
  14. http://guides.rubyonrails.org/getting_started.html

VIDEO: ISSUE 109 LAUNCH EVENT

Sponsors

  • Accenture
  • BT Code Crafters
  • Accesa
  • Bosch
  • Betfair
  • MHP
  • BoatyardX
  • .msg systems
  • P3 group
  • Ing Hubs
  • Cognizant Softvision
  • Colors in projects

VIDEO: ISSUE 109 LAUNCH EVENT