Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby: require vs require_relative - best practice to workaround running in both Ruby <1.9.2 and >=1.9.2
    primarykey
    data
    text
    <p>What is the best practice if I want to <code>require</code> a relative file in Ruby <em>and</em> I want it to work in both 1.8.x and >=1.9.2?</p> <p>I see a few options:</p> <ul> <li>just do <code>$LOAD_PATH &lt;&lt; '.'</code> and forget everything</li> <li>do <code>$LOAD_PATH &lt;&lt; File.dirname(__FILE__)</code></li> <li><code>require './path/to/file'</code></li> <li>check if <code>RUBY_VERSION</code> &lt; 1.9.2, then define <code>require_relative</code> as <code>require</code>, use <code>require_relative</code> everywhere where it's needed afterwards</li> <li>check if <code>require_relative</code> already exists, if it does, try to proceed as in previous case</li> <li>use weird constructions such as <code><pre><code>require File.join(File.dirname(__FILE__), 'path/to/file')</code></pre></code> - alas they don't seem to work in Ruby 1.9 throughly, because, for example: <pre><code><code>$ cat caller.rb require File.join(File.dirname(__FILE__), 'path/to/file') $ cat path/to/file.rb puts 'Some testing' $ ruby caller Some testing $ pwd /tmp $ ruby /tmp/caller Some testing $ ruby tmp/caller tmp/caller.rb:1:in 'require': no such file to load -- tmp/path/to/file (LoadError) from tmp/caller.rb:1:in '&lt;main&gt;'</code></code></pre></li> <li>Even weirder construction: <code><pre><code>require File.join(File.expand_path(File.dirname(__FILE__)), 'path/to/file')</code></pre></code> seems to work, but it's weird and not quite good looking.</li> <li>Use <a href="http://rubygems.org/gems/backports" rel="nofollow noreferrer">backports</a> gem - it's kind of heavy, it requires rubygems infrastructure and includes tons of other workarounds, while I just want <code>require</code> to work with relative files.</li> </ul> <p>There's a <a href="https://stackoverflow.com/questions/4325759/is-require-file-expand-path-file-the-best-practice">closely related question at StackOverflow</a> that gives some more examples, but it doesn't give a clear answer - which is a best practice.</p> <p>Is there are any decent, accepted-by-everyone universal solution to make my application run on both Ruby &lt;1.9.2 and >=1.9.2?</p> <h2>UPDATE</h2> <p>Clarification: I don't want just answers like "you can do X" - in fact, I've already mentioned most of choices in question. I want <em>rationale</em>, i.e. <em>why</em> it is a best practice, what are its pros and cons and why it should be chosen among the others.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload