Skip to content

Rodrigo Urubatan – About Code

Helping ruby developers to use the best tools for each job so they can solve hard problems, with less bugs and have more free time.

Menu
  • Home
  • My last presentations
  • About
  • Privacy Policy
Menu

Why Embrace Nesting in Ruby Modules?

Posted on 2024-05-21 by Rodrigo Urubatan

In Ruby, you have a choice: define classes or modules either inline or nested within another module. While both approaches achieve similar results, there are key functional differences to consider, making nesting the preferred approach.

The Subtle Distinction in Referencing

Let’s look at an example using User and Admin::User classes:

module Admin
  class Nested  # Nested approach
    def self.test = User.name
  end
end
 
class Admin::Inline  # Inline approach
  def self.test = User.name
end
 
puts Admin::Nested.test  # => "Admin::User" (correct)
puts Admin::Inline.test  # => "User" (incorrect)

Here, Admin::Nested correctly references Admin::User using User.name. However, Admin::Inline unexpectedly returns just “User”. This is because Ruby’s constant lookup prioritizes the current namespace first. Since Admin::Inline isn’t explicitly nested under Admin, it doesn’t search within that namespace for User.

To achieve the intended behavior with inline classes, you’d need to use the fully qualified name:

class Admin::InlineQualified
  def self.test = ::Admin::User.name  # Using fully qualified name
end
 
puts Admin::InlineQualified.test  # => "Admin::User" (correct)

Inheritance Nuances

The same concept applies to inheritance. Consider these BaseController classes:

class BaseController
end
 
module Admin
  class BaseController  # Nested approach
  end
end
 
puts Admin::NestedController.superclass  # => Admin::BaseController (correct)
puts Admin::InlineController.superclass  # => BaseController (incorrect)

Similar to the previous example, Admin::NestedController accurately retrieves its superclass Admin::BaseController. However, Admin::InlineController mistakenly returns the top-level BaseController.

Understanding Nesting’s Benefits

Nesting offers several advantages:

  1. Clarity and Explicitness: Nested modules clearly indicate the hierarchy and relationships between modules and classes.
  2. Correct Constant Resolution: Nesting ensures Ruby searches for constants within the appropriate namespace, preventing unexpected behavior.
  3. Automatic Module Creation: When defining a nested class like Api::UsersController, Ruby automatically creates the Api module if it doesn’t exist.

Recommendations and Best Practices

While inline modules can work with extra care (using fully qualified names), nesting is generally recommended for its clarity, predictability, and adherence to Ruby conventions. Static code analysis tools like Rubocop can enforce this practice, making it easy to integrate into your development workflow.

In Summary

By embracing nesting in Ruby modules, you gain a more organized, predictable, and robust codebase, avoiding potential gotchas and ensuring your code behaves as intended.

Related

Leave a Reply Cancel reply

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

Recent posts

  • Why Embrace Nesting in Ruby Modules?
  • An easy way to have a local “Github Copilot” for free
  • SPA without touching Javascript – The magic of Ruby on rails and Hotwire
  • I see Dead Jobs everywhere (sidekiq DeadSet)
  • Quick tips that help: rails notes

Arquives

  • May 2024
  • April 2024
  • February 2023
  • January 2023
  • December 2022
  • June 2021
  • March 2020
  • January 2020
  • July 2019
  • June 2019
  • May 2019
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • August 2015
  • August 2014
  • July 2014
  • August 2007

Categories

  • AI
  • articles
  • cfp
  • firebase
  • gems
  • git
  • opinion
  • presentations
  • projects
  • rails6
  • ruby
  • Sem categoria
  • server-api
  • tutorials
  • Uncategorized
© 2025 Rodrigo Urubatan – About Code | Powered by Minimalist Blog WordPress Theme