Difference between '.' and File.dirname(__FILE__) in RUBY - ruby

I am working my way through a Lynda.com Ruby course and building a small app.
In order to make the file portable, the teacher uses this form below.
APP_ROOT = File.dirname(__FILE__)
I understand the purpose of the constant FILE as it always resolves the current file no matter what its name.
When I type p APP_ROOT it resolves to a . # => '.' which I understand represents this current directory.
What I don't get is why, if File.dirname(__FILE__) always == '.', why not just use '.' ?
For example the output of:
$LOAD_PATH.unshift( File.join(APP_ROOT, 'lib'))
p $:
is equal to
$LOAD_PATH.unshift( File.join('.', 'lib'))
p $:
when I p $: I get the same exact results for either line. What is the value of File.dirname(__FILE__) over '.' ?
Also, I searched all over but I'm not sure.
If I am in directory /home/one/apps
If I enter '.'
is that equal to the apps directory OR does that mean the entire absolute path including the final directory? so . is really /home/ones/apps not just /apps
Thanks in advance.

File.dirname(__FILE__) isn’t always the same as the current directory. It will be the same if you start your Ruby program from the same directory as the file, but will be different if you start from a different directory.
For example if you have a subdirectory subdir containing foo.rb with the contents:
puts File.dirname(__FILE__)
then running from parent directory you get this:
$ ruby subdir/foo.rb
subdir
but if you cd into the directory and then run the file you get this:
$ cd subdir
$ ruby foo.rb
.

Related

Reading files elsewhere in directory w/ Ruby

I've got a project structure as follows:
info.config (just a JSON file w/ prefs+creds)
main.rb
tasks/
test.rb
In both main.rb (at the root of the project), and test.rb (under the tasks folder), I want to be able to read and parse the info.config file. I've figured out how to do that in main.rb with the following:
JSON.parse(File.read('info.config'))
Of course, that doesn't work in test.rb.
Question: How can I read the file from a test.rb even though it's one level deeper in the hierarchy?
Appreciate any guidance I can get! Thanks!
Use relative path:
path = File.join(
File.dirname(File.dirname(File.absolute_path(__FILE__))),
'info.config'
)
JSON.parse(File.read(path))
File.dirname(File.absolute_path(__FILE__)) will give you the directory where test.rb resides. -> (1)
File.dirname(File.dirname(File.absolute_path(__FILE__))) will give you parent directory of (1).
Reference: File::absolute_path, File::dirname
UPDATE
Using File::expand_path is more readable.
path = File.expand_path('../../info.config', __FILE__)
JSON.parse(File.read(path))
What I usually do is:
Create file called environment or similar in your project root. This file has only one purpose - to extend load path:
require 'pathname'
ROOT_PATH = Pathname.new(File.dirname(__FILE__))
$:.unshift ROOT_PATH
Require this file at the beginning of your code. From now on every time you call require, you can use relative_path to you root directory, without worrying where file you are requiring it from is located.
When using File, you can simple do:
File.open(ROOT_PATH.join 'task', 'test.rb')
You can do as below using File::expand_path :
path = File.expand_path("info.config","#{File.dirname(__FILE__)}/..")
JSON.parse(File.read(path))
File.dirname(__FILE__) will give you the path as "root_path_of_your_projet/tasks/".
"#{File.dirname(__FILE__)}/.." will give you the path as "root_path_of_your_projet/". .. means go one level up from the current directory.
File.expand_path("info.config","root_path_of_your_projet/") will give you the actual path to the file as "root_path_of_your_projet/info.config".
You can also use __dir__ instead of File.dirname(__FILE__).
__dir__ : Returns the canonicalized absolute path of the directory of the file from which this method is called.
Hope that explanation helps.

How do I require a Ruby file?

I have a file called "go.rb" that contains:
require 'turboname'
dictionary = Turboname::Random.new
100999032982389.times do
name = Turboname::Domain.new(:from => dictionary)
name.save if name.length < 15 and name.available?
tld = name.tldize
name.save(tld) if tld and name.length < 15 and name.available?(tld)
end
turboname.rb is located in the same directory as go.rb. It's the same level. I just want to include this file in this script. I don't want to deal with gems or bundles.
./turboname.rb:1:in `require': no such file to load -- turboname/version (LoadError)
from ./turboname.rb:1
from go.rb:1:in `require'
from go.rb:1
Use a require_relative Statement
Recent Ruby versions no longer add . to the load path stored in $:. However, one solution is to use Kernel#require_relative to require a file relative to the current value of __FILE__. For example:
require_relative './turboname'
Note that this doesn't work in interactive REPL sessions with irb or pry, but works fine within actual source files.
The error isn't telling you it can't find ./turboname.rb. It's telling you that it found that file, but the first line of ./turboname.rb tries to require 'turboname/version', which Ruby can't find. Does ./turboname/version.rb exist? If so, is it readable by the current user?
If everything else checks out, then you have a load-path problem. At the top of go.rb, explicitly add the current working directory (or whichever directory contains turboname.rb and turboname/version.rb (possibly ./lib/) to your load path:
$LOAD_PATH << File.dirname(__FILE__) # for ./
# or
$LOAD_PATH << File.join(File.dirname(__FILE__), 'lib') # for ./lib/
With Ruby 2.0:
require "#{__dir__}/turboname"

Confusing behavior of File.dirname

I have written a couple of small Ruby scripts for system administration using Ruby 1.9.3. In one script I use:
File.dirname(__FILE__)
to get the directory of the script file. This returns a relative path, however when I call the script from a second script File.dirname returns an absolute path.
Ruby Doc lists an absolute return path in its example whereas I found a discussion on Ruby Forum where a user says dirname should only return a relative path.
I am using the suggested solution from Ruby Forums to use File.expand_path to always get the absolute path like this:
File.expand_path(File.dirname(__FILE__))
but is there a way to make the behaviour of dirname consistent?
UPDATE:
To expand on Janathan Cairs answer, I made two scripts:
s1.rb:
puts "External script __FILE__: #{File.dirname(__FILE__)}"
s0.rb:
puts "Local script __FILE__: #{File.dirname(__FILE__)}"
require './s1.rb'
Running ./s0.rb gives the following output:
Local script __FILE__: .
External script __FILE__: /home/kenneth/Pictures/wp/rip_vault
File.dirname should return an absolute path if given an absolute path, and a relative path if given a relative path:
File.dirname('/home/jon/test.rb') # => '/home/jon'
File.dirname('test.rb') # => '.'
__FILE__ returns the name of the current script, which is therefore a relative path from the current directory. That means you should always use expand_path if you want to get the absolute path with File.dirname(__FILE__).
NB Ruby 2.0.0 introduces the __dir__ constant
If you already upgraded to Ruby 2.0, you can use the new constant
__dir__
otherwise you can use
File.expand_path('..', __FILE__)
which is shorter than
File.expand_path(File.dirname(__FILE__))
File.expand_path documentation

Ruby 1.9.3 - Locate file local to ruby program regardless of where program is called from

I've been working on my first Ruby project, and in the process of trying to organize my files into different directories, I've run into trouble with having .rb files load non-ruby files (e.g. .txt files) local to themselves.
For example, suppose a project has the following structure:
myproject/
bin/
runner.rb
lib/
foo.rb
fooinfo.txt
test/
testfoo.rb
And the file contents are as follows:
runner.rb
require_relative '../lib/foo.rb'
foo.rb
File.open('./fooinfo.txt') do |file|
while line = file.gets
puts line
end
end
If I cd to lib and run foo.rb, it has no trouble finding fooinfo.txt in its own directory and printing its contents.
However, if I cd to bin and run runner.rb, I get
in `initialize': No such file or directory - ./fooinfo.txt (Errno::ENOENT)
I assume this is because File.open searches relative to whatever directory the top level program is run from.
Is there a way to ensure that foo.rb can find fooinfo.rb regardless of where it is run/required from (assuming that foo.rb and fooinfo.rb always maintain the same location relative to eachother)?
I'd like to be able to run foo.rb from bin/runner.rb, and a test file in test/, and have it be able to find fooinfo.txt in both cases.
Ideally, I'd like to have a solution that would work even if the entire myproject directory were moved.
Is there something like require_relative that can locate a non-ruby file?
Try using __FILE__ and File.dirname to build absolute paths. For example:
File.open(File.expand_path(File.dirname(__FILE__)) + './fooinfo.txt') do |file|
...
end
In this case, the simplest thing is to just change
File.open('./fooinfo.txt')
to
File.open('../lib/fooinfo.txt')
That will work from from any project subdirectory directly under your project root (including lib/).
The more robust solution, useful in larger projects, is to have a PROJECT_ROOT constant that you can use from anywhere. If you have lib/const.rb:
module Const
PROJECT_ROOT = File.expand_path("..", File.dirname(__FILE__))
end
Then (assuming you've requireed that file) you can use:
File.open(Const::PROJECT_ROOT + '/lib/fooinfo.txt')

What does $:<< "." do to Ruby's require path?

I don't understand the meaning of $:<< "." in Ruby.
I upgraded Ruby to 1.9.1, but a program was not working. My classmate told me that I am supposed to add $:<< "."
What does $:<< "." do?
$: is the variable that holds an array of paths that make up your Ruby's load path
<< appends an item to the end of the array
. refers to the current directory
1 2 3
| | |
V V V
$: << "."
So you are adding the current directory to Ruby's load path
References:
Can be found in the Execution Environment Variables section of of this page from The Pragmatic Programmers Guide
An array of strings, where each string specifies a directory to be searched for Ruby scripts and binary extensions used by the load and require methods. The initial value is the value of the arguments passed via the -I command-line option, followed by an installation-defined standard library location, followed by the current directory (“.”)[Obviously this link is for an older version of Ruby as this is still in there]. This variable may be set from within a program to alter the default search path; typically, programs use $: << dir to append dir to the path.
Can be found in the docs for array at ruby-doc.org.
Append—Pushes the given object on to the end of this array. This expression returns the array itself, so several appends may be chained together.
Since version 1.9, Ruby doesn't look for required files in the current working directory AKA .. The $LOAD_PATH or $: global variable is an array of paths where Ruby looks for files you require.
By adding $:<< "." to your files, you are actually telling Ruby to include your current directory in the search paths. That overrides new Ruby behavior.
In your example you add working directory (".") to ruby load path ($:).
Working directory (".") was removed from load path (global variable $: or $-I or $LOAD_PATH) in Ruby 1.9 because it was considered a security risk:
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:
==Project1/main1.rb:
$: << "."
require 'init'
==Project1/init.rb:
puts 'init 1'
And you have alike project:
==Project2/main2.rb:
$: << "."
require 'init'
==Project2/init.rb:
puts 'init 2'
If you run Project1 from Project2 folder, then main1.rb will require Project2/init.rb, not Project1/init.rb:
~/Projects/Project2$ ruby ../Project1/main1.rb
init 2 # may be unexpected an dangerous
~/Projects/Project2$ ruby main2.rb
init 2
You can change your working directory in your code, e.g. using Dir.chdir:
ruby-1.9.2-p290 :002 > puts File.expand_path('.')
=> /home/alex/Projects
ruby-1.9.2-p290 :003 > Dir.chdir('..')
ruby-1.9.2-p290 :004 > puts File.expand_path('.')
=> /home/alex
I recommend you to use the following techniques instead of $: << '.':
require_relative (Ruby 1.9 only)
Add folder of the file to the working directory (common approach because it is compatible with Ruby 1.8): $: << File.expand_path('..', __FILE__) etc.. __FILE__ is a reference to the current file name. File.expand_path converts a pathname to an absolute pathname.

Resources