From: Josh Cheek Date: 2010-05-15T18:27:29+09:00 Subject: Re: difference between $0 and __FILE__ --0016367b61e87ef1b804869e99ac Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable 2010/5/15 Une B=E9vue > > 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 =3D /Users/yt/bin/path_test.rb > __FILE__ =3D /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 ? > -- > =AB Je suis s=FBr que la ga=EEt=E9 ambigu=EB du dr=F4le de gar=E7on qui = a bu du > whisky et fait le z=E8bre sur un vieux ca=EFman =E0 No=EBl dans le ca=F1o= n, a =E9t=E9 > b=E9n=E9fique =E0 l'=E2me du po=E8te bl=EAme, ainsi qu'=E0 son c=9Cur & c= =E6tera ! =BB > (=A9 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: http://ruby-doc.org/core/classes/File.html#M002540 http://tomafro.net/2010/01/tip-relative-paths-with-file-expand-path In this case, that probably means you want File.expand_path('ruby_ext/ansi_color',__FILE__) --0016367b61e87ef1b804869e99ac--