Where to put code when monkey patching

喜欢而已 提交于 2020-05-26 12:23:45

问题


Everything I read about monkey patching says to do something like this:

class String
  def foo
    #your special code 
  end
end

But I can't find any instructions on where to put this code. In a rails app, can I just put this any crazy place I want? In a Module? A Model?

Do I need to include something in the file where I define my monkeypatch? Do I need to include my monkeypatch everywhere where I want to use it?


回答1:


There is no set rule on this. Technically you can open it (the class; and add your method) anywhere. I usually make a special file called monkey_patches.rb and put it in config/initializers or in a misc folder in my Rails app so if theres ever a conflict I know where to look.

Also I'd advise to use a Module to wrap the monkey patch. Check out 3 ways to monkey patch without making a mess for more info.

His example:

module CoreExtensions
  module DateTime
    module BusinessDays
      def weekday?
        !sunday? && !saturday?
      end
    end
  end
end

DateTime.include CoreExtensions::DateTime::BusinessDays



回答2:


I have used the following technique described by Justin Weiss in 3 Ways to Monkey-Patch Without Making a Mess

When in vanilla Ruby, a gem, for instance, you define a module in some file you are requiring and then include (or extend) the module into desired class.

module StringMonkeypatch
  def foo
    #your special code 
  end
end

String.include StringMonkeypatch

When in Rails you may want to define the module in a place that gets autoloaded (look up autoload_paths) and in a way that follows Rails' naming convention.

For example, if monkeypatching the Sidekiq::Testing gem class you should mirror the file structure.

# in /app/<something telling>/sidekiq/testing/monkeypatch.rb
module Sidekiq::Testing::Monkeypatch
  def foo
    #your special code 
  end
end

# in /config/environment.rb, at the bootom
Sidekiq::Testing.include Sidekiq::Testing::Monkeypatch



回答3:


Just chiming in because it took me forever to figure this out because very little solutions worked.

• I had to use plain old require. I put it in the config/application.rb file. The file does not automatically load for me if you put it in the app directory like some are suggesting. I don't know why.

patching_file_path = File.expand_path("./lib", Dir.pwd) Dir[patching_file_path+'/*.rb'].each {|file| require file }

• I also put a temporary puts "I'm Working! in the file I'm trying to require so I can check the console to see if it's actually loading.

• Also, if you're using spring loader, before you start your console you should do bin/spring stop in your terminal before you start your rails console. Otherwise, it won't load new files.



来源:https://stackoverflow.com/questions/41705391/where-to-put-code-when-monkey-patching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!