Difference between $0 and __FILE__

i have a script in my HOME/bin :

/Users/yt/bin/path_test.rb

my "HOME/bin" is in the PATH

then, if from this script, i outputs $0 and __FILE__

with :

path_test.rb i got :
$0 = /Users/yt/bin/path_test.rb
__FILE__ = /Users/yt/bin/path_test.rb

and the same outputs with :
/Users/yt/bin/path_test.rb

why do i have the same outputs in this case ?

in fact i'm looking that because i do have Ruby class extension in a
subfolder of "HOME/bin", namely :
HOME/bin/ruby_ext
where i put my extension for classes of which i could require :
require "#{File.dirname($0)}/ruby_ext/ansi_color"

what's the best way to require in that case, does i need an "absolute
path (starting from / ) or not ?

···

--
« Je suis sûr que la gaîté ambiguë du drôle de garçon qui a bu du
whisky et fait le zèbre sur un vieux caïman à Noël dans le cañon, a été
bénéfique à l'âme du poète blême, ainsi qu'à son cœur & cætera ! »
(© Jean-Paul Blanc)

Having a hard time understanding your question. Here are some possible
thoughts:

Difference between $0 and __FILE__
  $ echo 'p $0 , __FILE__' > callee0.rb
  $ echo 'p $0 , __FILE__' > callee1.rb
  $ echo 'p $0 , __FILE__' > callee2.rb
  $ echo '3.times { |i| require "callee#{i}" ; puts }' > main_prog.rb
  $ ruby main_prog.rb
  "main_prog.rb"
  "./callee0.rb"

  "main_prog.rb"
  "./callee1.rb"

  "main_prog.rb"
  "./callee2.rb"

You have the same output because
  The program that was run is the same as the file being checked

How to require relative files:
  class File - RDoc Documentation
  Tip: Relative paths with File.expand_path - Tom Ward's Blog
  In this case, that probably means you want
File.expand_path('ruby_ext/ansi_color',__FILE__)

···

2010/5/15 Une Bévue <unbewusst.sein@google.com.invalid>

i have a script in my HOME/bin :

/Users/yt/bin/path_test.rb

my "HOME/bin" is in the PATH

then, if from this script, i outputs $0 and __FILE__

with :

path_test.rb i got :
$0 = /Users/yt/bin/path_test.rb
__FILE__ = /Users/yt/bin/path_test.rb

and the same outputs with :
/Users/yt/bin/path_test.rb

why do i have the same outputs in this case ?

in fact i'm looking that because i do have Ruby class extension in a
subfolder of "HOME/bin", namely :
HOME/bin/ruby_ext
where i put my extension for classes of which i could require :
require "#{File.dirname($0)}/ruby_ext/ansi_color"

what's the best way to require in that case, does i need an "absolute
path (starting from / ) or not ?
--
« Je suis sûr que la gaîté ambiguë du drôle de garçon qui a bu du
whisky et fait le zèbre sur un vieux caïman à Noël dans le cañon, a été
bénéfique à l'âme du poète blême, ainsi qu'à son cœur & cætera ! »
(© Jean-Paul Blanc)

i have a script in my HOME/bin :

/Users/yt/bin/path_test.rb

my "HOME/bin" is in the PATH

then, if from this script, i outputs $0 and __FILE__

with :

path_test.rb i got :
$0 = /Users/yt/bin/path_test.rb
__FILE__ = /Users/yt/bin/path_test.rb

and the same outputs with :
/Users/yt/bin/path_test.rb

why do i have the same outputs in this case ?

Because the shell will also expand full names. How does your PATH look like? I assume you set something like PATH="${PATH}:${HOME}/bin" - in that case you'll have the absolute path to ~/bin in your PATH and consequently the script is invoked with absolute path.

in fact i'm looking that because i do have Ruby class extension in a
subfolder of "HOME/bin", namely :
HOME/bin/ruby_ext
where i put my extension for classes of which i could require :
require "#{File.dirname($0)}/ruby_ext/ansi_color"

what's the best way to require in that case, does i need an "absolute
path (starting from / ) or not ?

IMHO the best way is to define a local location of library files and set RUBYLIB to that directory (I use "$HOME/lib/ruby" for that because I prefer to have lib code separate from programs, but in your case you could also use "$HOME/bin").

If you want to do it on a per script basis you could do this at the beginning of your script (i.e. before any requires):

$:.unshift "/your/folder/here"

Kind regards

  robert

···

On 15.05.2010 10:33, Une Bévue wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Because the shell will also expand full names. How does your PATH look
like? I assume you set something like PATH="${PATH}:${HOME}/bin" - in
that case you'll have the absolute path to ~/bin in your PATH and
consequently the script is invoked with absolute path.

> in fact i'm looking that because i do have Ruby class extension in a
> subfolder of "HOME/bin", namely :
> HOME/bin/ruby_ext
> where i put my extension for classes of which i could require :
> require "#{File.dirname($0)}/ruby_ext/ansi_color"
>
> what's the best way to require in that case, does i need an "absolute
> path (starting from / ) or not ?

OK, i see.

IMHO the best way is to define a local location of library files and set
RUBYLIB to that directory (I use "$HOME/lib/ruby" for that because I
prefer to have lib code separate from programs, but in your case you
could also use "$HOME/bin").

If you want to do it on a per script basis you could do this at the
beginning of your script (i.e. before any requires):

$:.unshift "/your/folder/here"

fine thanks !

···

Robert Klemme <shortcutter@googlemail.com> wrote:
--
« Je suis sûr que la gaîté ambiguë du drôle de garçon qui a bu du
whisky et fait le zèbre sur un vieux caïman à Noël dans le cañon, a été
bénéfique à l'âme du poète blême, ainsi qu'à son cœur & cætera ! »
(© Jean-Paul Blanc)