Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ul> <li><p><code>require blaba.rb</code> is searching to your <em>default gem path</em> to load the file, which depends on the ruby version you are using. For example RVM will search in <code>$HOME/.rvm/rubies/...</code> while a system wide ruby will search in the distribution's default path. Note that this is where gems are located, but you could manually add a library say <code>mylibrary.rb</code> in the same path and use it in any of your programs. However, that's an awful thing to do, it's a much cleaner procedure to create gems and install them in your system.</p></li> <li><p><code>require ./blabla.rb</code> loads a file that is sitting in your <em>working directory</em>. You could add the full path like <code>require /home/username/library/myproject/models/sample.rb</code>. It will work just about the same. In the UNIX-like world the <code>./</code> sign means <em>current directory</em>. This solution is often used in <code>irb</code> to load say a rails Model i.e. <code>users.rb</code> into <code>irb</code> or <code>pry</code> and work with it. To give you an example in a shell environment (if you are familiar with UNIX shells, you'll figure it out):</p> <pre><code>GreyJewel ~ » ls myports.txt myports.txt GreyJewel ~ » ls ./myports.txt ./myports.txt </code></pre></li> <li><p>The third solution <code>require File.expand_path('../sample.rb', __FILE__)</code> is used in programs, because it explicitly creates a <strong>full path</strong> using as an anchor the directory which the file holding the line sits, which is a much more secure approach compared to <code>require ./sample.rb</code>. Note that when you load a ruby file, you can omit the file extension <code>.rb</code>.</p></li> </ul> <p>Hope this clarifies a bit the situation.</p>
 

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