Blog

Ruby on Rails Conventions

Rodrigo Assis
February 10, 2023

In this article we’ll discuss some of Ruby on Rails conventions and why they are still relevant and alive today.

Rails Exists Because of Ruby

Ruby is different because it values different things, and most of those things are related to ensuring programmer happiness.

It’s interesting to see how Ruby was described in the early days: ‘The Principle of Least Surprise”

Ruby should behave how you’d expect it. This is easily described with a contrast to Python:


$ irb
irb(main):001:0> exit
$ irb
irb(main):001:0> quit

$ python
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit

Ruby accepts both exit and quit to accommodate the programmer’s obvious desire to quit its interactive console. Python, on the other hand, instructs the programmer how to properly do what’s requested, even though it obviously knows what is meant (since it’s displaying the error message).

Convention over Configuration

Moving from configuration to convention gives us reusable patterns that can apply across multiple areas of your project. This allows for better abstractions and leads to better ideas.

If we can depend on a Person class mapping to a people table, we can use that same inflection to map an association declared as has_many :people to look for a Person class. The power of good conventions is that they pay dividends across different situations.

But beyond the productivity gains for experts, conventions also lower the barriers of entry for beginners. There are so many conventions in Rails that a beginner doesn’t even need to know about, but can just benefit from. It’s possible to create great applications without knowing why everything is the way it is.

Beautiful Code is Relevant

Eye pleasing code is a value unto itself and should be pursued.

So what is beautiful code? Here’s a simple example from Active Record:


class Project < ApplicationRecord
  belongs_to :account
  has_many :participants, class_name: 'Person'
  validates_presence_of :name
end

There’s nothing fancy here, but it’s surely pretty and simple.

Conclusion

Ruby on Rails is more than just a framework on top of a programming language that makes you more productive. It’s a philosophy on how to build solutions for real world problems without losing the fun of it!

Hope you enjoyed this post :)