Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your example you add working directory (<code>"."</code>) to ruby load path (<code>$:</code>).</p> <p>Working directory (<code>"."</code>) was removed from load path (global variable <code>$:</code> or <code>$-I</code> or <code>$LOAD_PATH</code>) in Ruby 1.9 because it was considered a security risk:</p> <ul> <li>Your working directory may be any folder, and your script will require files from this folder if these files have appropriate names. For example you have 2 files in Project1 folder main.rb and init.rb:</li> </ul> <blockquote> <p>==Project1/main1.rb:<br/> $: &lt;&lt; "." require 'init'<br/> ==Project1/init.rb:<br/> puts 'init 1' </p> </blockquote> <p>And you have alike project:</p> <blockquote> <p>==Project2/main2.rb:<br/> $: &lt;&lt; "." require 'init'<br/> ==Project2/init.rb:<br/> puts 'init 2' </p> </blockquote> <p>If you run Project1 from Project2 folder, then main1.rb will require Project2/init.rb, not Project1/init.rb:</p> <blockquote> <p>~/Projects/Project2$ ruby ../Project1/main1.rb<br/> init 2 # may be unexpected an dangerous<br/> ~/Projects/Project2$ ruby main2.rb<br/> init 2<br/></p> </blockquote> <ul> <li><p>You can change your working directory in your code, e.g. using <code>Dir.chdir</code>:</p> <pre><code>ruby-1.9.2-p290 :002 &gt; puts File.expand_path('.') =&gt; /home/alex/Projects ruby-1.9.2-p290 :003 &gt; Dir.chdir('..') ruby-1.9.2-p290 :004 &gt; puts File.expand_path('.') =&gt; /home/alex </code></pre></li> </ul> <p>I recommend you to use the following techniques instead of <code>$: &lt;&lt; '.'</code>:</p> <ul> <li><p><a href="http://extensions.rubyforge.org/rdoc/classes/Kernel.html#M000039" rel="nofollow">require_relative</a> (<em>Ruby 1.9 only</em>)</p></li> <li><p>Add folder of the file to the working directory (common approach because it is compatible with <em>Ruby 1.8</em>): <code>$: &lt;&lt; File.expand_path('..', __FILE__) etc.</code>. <code>__FILE__</code> is a reference to the current file name. <a href="http://www.ruby-doc.org/core-1.9.3/File.html#method-c-expand_path" rel="nofollow">File.expand_path</a> converts a pathname to an absolute pathname.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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