{"id":71,"date":"2008-05-16T16:24:11","date_gmt":"2008-05-16T22:24:11","guid":{"rendered":"http:\/\/www.pervasivecode.com\/blog\/2008\/05\/16\/rails-snippet-require-app-files-only-once\/"},"modified":"2008-08-27T10:09:24","modified_gmt":"2008-08-27T16:09:24","slug":"rails-snippet-require-app-files-only-once","status":"publish","type":"post","link":"http:\/\/www.pervasivecode.com\/blog\/2008\/05\/16\/rails-snippet-require-app-files-only-once\/","title":{"rendered":"Rails snippet: require app files only once"},"content":{"rendered":"<p>Ruby&#8217;s Kernel.require method will re-require the same source file if you pass it differing arguments that point to the same file. It doesn&#8217;t use File.expand_path to make sure it hasn&#8217;t already loaded the same file before. This can cause problems if you&#8217;re using constants or doing one-time initialization in a source file that&#8217;s getting loaded multiple times for one reason; you&#8217;ll need to add a wrapper that prevents re-entry.<\/p>\n<p>If this annoys you as much as it does me (why should my application code include workarounds for what I regard to be a Ruby bug?), add this snippet to your config\/environment.rb (pre Rails 2.0) or config\/preinitializer.rb (Rails 2.0 or later).<br \/>\n<!--more--><\/p>\n<pre>\r\nmodule Kernel\r\n    def require_with_expand_path(source_file)\r\n        expanded = File.expand_path(source_file)\r\n        is_app_file = File.file?(expanded)\r\n        require_without_expand_path(is_app_file ? expanded : source_file)\r\n    end\r\n    alias_method 'require_without_expand_path', 'require'\r\n    alias_method 'require', 'require_with_expand_path'\r\nend\r\n\r\n<\/pre>\n<p>The is_app_file check may not be the best way to test for it; I can see how if you fiddle with $: so that the current directory files load after some other directory, you might load things in the wrong order. Please post fixes if you find this to be a real problem and have an idea of how to fix it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ruby&#8217;s Kernel.require method will re-require the same source file if you pass it differing arguments that point to the same file. It doesn&#8217;t use File.expand_path to make sure it hasn&#8217;t already loaded the same file before. This can cause problems if you&#8217;re using constants or doing one-time initialization in a source file that&#8217;s getting loaded &hellip; <a href=\"http:\/\/www.pervasivecode.com\/blog\/2008\/05\/16\/rails-snippet-require-app-files-only-once\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Rails snippet: require app files only once&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,20],"tags":[],"class_list":["post-71","post","type-post","status-publish","format-standard","hentry","category-ruby","category-ruby-on-rails"],"_links":{"self":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/71"}],"collection":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/comments?post=71"}],"version-history":[{"count":0,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}