Skip to content
Menu
Railshero
  • Blog
  • About Me
  • Contact Me
  • Privacy Policy
Railshero
railshero.pw dont repeat yourself DRY

Understanding the DRY Principle in Rails

Posted on July 2, 2023 by Rails Hero

Embarking on a journey through the world of Ruby on Rails (Rails), aspiring software developers inevitably stumble upon a multitude of programming conventions and methodologies. The DRY principle, an acronym for “Don’t Repeat Yourself,” is a fundamental precept echoing loudly throughout Rails terrain. While Rails isn’t the sole benefactor of this rule, the DRY philosophy is undoubtedly instrumental in crafting succinct, easy-to-manage code across various programming languages and frameworks.

Unraveling the DRY Principle

“Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.” – The Pragmatic Programmer.

Originating from the minds of Andrew Hunt and David Thomas in their book “The Pragmatic Programmer”, the DRY principle urges developers to circumvent code duplication. Spotting the same piece of code in multiple places signals an opportunity for code abstraction, thereby making it reusable. Importantly, the DRY principle isn’t confined to functions or chunks of code—it permeates databases, test blueprints, software builds, and even documentation.

Adhering to the DRY principle improves your code in several ways:

  1. Maintainability: Any necessary modification can be performed at a single point, eliminating the frantic search for multiple instances of the same code scattered throughout the application.
  2. Readability: Code that adheres to the DRY principle often gives rise to modular and more intuitive code architectures.
  3. Bug minimization: By reusing code, you also recycle the testing and debugging efforts, thus mitigating the chances of bug appearances.

DRY in Ruby

Ruby offers several avenues to comply with the DRY principle, such as the utilization of methods, modules, and inheritance.

Methods

Ruby provides a feature to create methods, which are encapsulated procedures designed to perform specific tasks. If a particular set of code lines is frequently used, defining a method for these operations and invoking it as needed can help.

For example, we have the “greet” method, which is utilized to wrap the process of creating a greeting string:

def greet(name)
"Hello, #{name}!"
end

puts greet("Alice")
puts greet("Bob")

Modules

Modules allow the bundling of related methods, constants, and classes. If methods are shared across multiple classes, they can be abstracted into a module:

module Greetable
def greet(name)
"Hello, #{name}!"
end
end

class Person
include Greetable

def initialize(name)
@name = name
end

def greet_self
greet(@name)
end
end

alice = Person.new("Alice")
puts alice.greet_self

Here, the “greet” method is relocated into a module named “Greetable.” The “Person” class then imports this module, gaining access to the “greet” method.

Inheritance

Inheritance is another effective mechanism to keep your Ruby code DRY. When multiple classes exhibit common attributes or methods, they can derive from a shared parent class:

class Animal
def initialize(name)
@name = name
end

def greet
"Hello, I am a #{@name}."
end
end

class Dog < Animal
def bark
"Woof!"
end
end

fido = Dog.new("Fido")
puts fido.greet
puts fido.bark

In this scenario, the “Dog” class inherits from the “Animal” class, thereby gaining access to the “initialize” and “greet” methods, hence avoiding code repetition in the “Dog” class.

DRY in Rails

Rails has the DRY principle embedded at its core. Rails ensures adherence to DRY through numerous mechanisms, such as Convention over Configuration, Active Record, partial views, helpers, and concerns.

Convention over Configuration (CoC)

CoC is a design paradigm incorporated by Rails to minimize the decisions a developer must make. Rails provides a set of sensible defaults that prove efficient in most situations, thereby eliminating repetitive tasks and maintaining the DRY principle.

Active Record

Active Record, Rails’ integrated Object-Relational Mapping (ORM) layer, is an excellent manifestation of the DRY principle. Instead of repeatedly scripting SQL queries, you can leverage Ruby methods provided by Active Record. For instance, to extract all records from the users table, instead of composing SQL queries each time, you can simply write:

users = User.all

Partial Views

Rails allows you to create reusable code fragments, known as partials, in your views. If an HTML code snippet is common across multiple views, it can be abstracted into a partial and rendered in your views.

For example, a form appearing in multiple views can be abstracted into a _form.html.erb partial:

<%= form_with model: @user do |form| %>
<%= form.label :name %>
<%= form.text_field :name %>

<%= form.label :email %>
<%= form.text_field :email %>

<%= form.submit %>
<% end %>

And then rendered in your views:

<%= render 'form' %>

Helpers

Rails offers helper methods for use in your views to uphold the DRY principle. For instance, instead of manually writing identical HTML links, you can employ the “link_to” helper:

<%= link_to 'Home', root_path %>

Additionally, you can define your own customized helper methods in the app/helpers directory.

Concerns

Concerns in Rails are modules that facilitate extraction of reusable code pieces from your models, controllers, or mailers. If several models share common associations or methods, you can extract that into a concern.

module Commentable
extend ActiveSupport::Concern

included do
has_many :comments, as: :commentable
end
end

class Post < ApplicationRecord
include Commentable
end

class Photo < ApplicationRecord
include Commentable
end

In this case, the “Commentable” concern is incorporated in both the “Post” and “Photo” models, eliminating the necessity to write the “has_many” association in each model.


Final Thoughts

Adherence to the DRY principle yields robust, maintainable code. While the principle may appear straightforward, its effective execution can pose challenges, and over-abstraction can lead to convoluted, hard-to-trace code. Thus, in your pursuit of keeping your code DRY, balance is vital – equilibrium between readability and simplicity.

Ruby on Rails offers a multitude of ways to adhere to the DRY principle. Developing a firm understanding of these strategies will aid you in creating top-tier, easily maintainable web applications.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tags

active record active storage assets assign to many associations attachment attachments authentication authorization avatar bootstrap cdn config database deploy deployments devise DRY environment variables file uploads Gemfile gems has_many helpers heroku index indexing initializer javascript pagination parameters postgres production public routes.rb ruby gem ruby gems search sendgrid server smtp stylesheets variants views voting

Recent Posts

  • Understanding the DRY Principle in Rails
  • Building Multi-tenant Applications with Rails
  • Rails Basics: Templating Engines
  • Deploying With Capistrano
  • Automated Testing in Rails

Archives

  • July 2023
  • June 2023
  • October 2021
  • September 2021
  • August 2021
  • July 2021

Categories

  • Active Record
  • Activity Logging
  • Apps
  • Assets
  • Attachments
  • Audio
  • Authentication
  • Authorization
  • Deployments
  • Error Pages
  • File Uploads
  • General
  • Heroku
  • Heroku
  • Pagination
  • Rails Basics
  • RubyGems
  • Search Engine Optimization
  • Search/Indexing
  • Testing
  • User Interface
  • Video
  • Views & Templating
  • Voting
  • Web Security
©2025 Railshero | Theme: Wordly by SuperbThemes
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT