Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6211421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:20:03+00:00 2026-05-24T06:20:03+00:00

I’ve got a library module I’d like to override based on the rails environment

  • 0

I’ve got a library module I’d like to override based on the rails environment I’m running in

Module is located in lib/package/my_module.rb:

module Package
  module MyModule
    puts "Defining original module"
    def foo
      puts "This is the original foo"
    end
  end
end

I have been able to partially solve with the info at Overriding a module method from a gem in Rails – specifically, in my environments/dev_stub.rb:

Package::MyModule.module_eval do
 puts "Defining override"
 def foo
   puts "This is foo override"
 end
end

(The other solution at that link seems to cause errors when rails tries to lookup other classes related to package)

Now, this seems to get me most of the way there, and works if I set

config.cache_classes = true

…but I want to use this as a stub development environment, and the comment recommendation on this value for a dev environment is to use false… in which case the override only works the first time the module is included, and any subsequent times, it uses the original.

My question: Am I going about this the right way? I could hack up the lib module itself to conditionally override based on RAILS_ENV, but I’d like to keep it cleaner than that…

Edit

My use case for this is to reference it from a controller function. If I have

class SomethingController < ApplicationController
  def show
    Package::MyModule.foo
  end
end

and config.cache_classes=false (which I ideally want since it is a development environment), and access the action through my web browser (http://localhost/something/show) then the first time I hit it, my override is loaded and it works, but the second and any subsequent times, the original library class is reloaded (outputs “Defining original module” on my console without “Defining override”), and the override is lost.

Another alternative I tried was add something like config.load_paths += %W( #{RAILS_ROOT}/lib_patch/#{RAILS_ENV}) to environment.rb – but defining the same module/class didn’t quite work without putting in an explicit hook in the original library to basically load the patch if it existed

Edit 2 (in response to @apneadiving answer)

I’ve tried doing this without module_eval, and just using the following in development_stub.rb:

require 'package/my_module'
module Package
  module MyModule
    puts "Defining override"
    def foo
      puts "This is foo override"
    end
  end
end

The problem I initially had with doing this is that Rails no longer automatically finds all content in my lib directory, and I need to sprinkle ‘require’ statements throughout all other lib files (and my controllers that reference the libs) to cover all of their dependencies. Although this is all done, it does work, but it also has a similar effect as config.cache_classes=true does, in that all the lib classes are not reloaded on change, even in my regular development environment that does not have a monkey-patch (since all the ‘require’ statements are added).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:20:04+00:00Added an answer on May 24, 2026 at 6:20 am

    Setting config.cache_classes=true in dev_stub.rb and using module_eval to define the patch as described in the question seems the way to go for what the goal is here – to create an environment specific patch for a module that doesn’t impact the other environments in both code path and Rails class loading behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.