[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

getting the name of the script in use

pere.noel

9/30/2006 6:00:00 PM


I'd like getting the name of the script in use, if i make use of $0 i
get the whole path of the script, ie :

/path/to/script/the_script.rb

no way to get only "the_script.rb"

(without using file basename ?)
--
une bévue
14 Answers

pere.noel

9/30/2006 6:57:00 PM

0

David Vallner <david@vallner.net> wrote:

> In the executed script, __FILE__ will contain the same as $0. Cf the "if
> $0 == __FILE__ main() end" idiom.


ok, thanks.

> What's wrong with using basename again?

nothing at all, i was dreaming of a direct way to gzt thz script name
alone (for the usage message)....

--
une bévue

MonkeeSage

9/30/2006 7:22:00 PM

0

Une bévue wrote:
> nothing at all, i was dreaming of a direct way to gzt thz script name
> alone (for the usage message)....

I think this is pretty direct: File.basename($0) ... perhaps you meant
immediate?

Regards,
Jordan

pere.noel

9/30/2006 8:18:00 PM

0

MonkeeSage <MonkeeSage@gmail.com> wrote:

> I think this is pretty direct: File.basename($0) ... perhaps you meant
> immediate?

yes ! i knew File.basenamepath)...
--
une bévue

MonkeeSage

9/30/2006 11:22:00 PM

0

Une bévue wrote:
> yes ! i knew File.basenamepath)...

I know...I saw your first post. But that seems very direct to me. I
think you meant immediate, i.e., without calling a method. There is no
way to do that, that I know of anyway, but you can always do it
yourself (you could even make a file just for that):

### direct.rb ###
$base = File.basename($0)

### somethingelse.rb ###
require 'direct'
puts $base

I do something similar to find the real path of a script:

### realpath.rb ###
# This basically does the same thing as:
# require 'pathname'
# File.dirname(Pathname.new($0).realpath)
$realpath = File.expand_path($0)
if File.symlink?($realpath)
$realpath = File.readlink($realpath)
end
$realpath = File.dirname($realpath)

### somethingelse.rb ###
require 'realpath'
puts $realpath

Regards,
Jordan

pere.noel

10/1/2006 4:15:00 AM

0

MonkeeSage <MonkeeSage@gmail.com> wrote:

> I know...I saw your first post. But that seems very direct to me. I
> think you meant immediate, i.e., without calling a method. There is no
> way to do that, that I know of anyway, but you can always do it
> yourself (you could even make a file just for that):
>
> ### direct.rb ###
> $base = File.basename($0)
>
> ### somethingelse.rb ###
> require 'direct'
> puts $base

in ruby what's the meaning of "$" before base ?
a way to get it as global var ?

> I do something similar to find the real path of a script:
>
> ### realpath.rb ###
> # This basically does the same thing as:
> # require 'pathname'
> # File.dirname(Pathname.new($0).realpath)
> $realpath = File.expand_path($0)
> if File.symlink?($realpath)
> $realpath = File.readlink($realpath)
> end
> $realpath = File.dirname($realpath)
>
> ### somethingelse.rb ###
> require 'realpath'
> puts $realpath


Right, nice idae, i do have a folder "rb" in my HOME/bin where i put
some small ruby scripts like that and some ruby object extension such as
string.

quit frankly i was wrong i've believe the behaviour of shell scripts is
different then, i've made a riny test in zsh (my prefered shell) :

#!/usr/bin/env zsh
echo $0
exit 0

and i get, as for ruby :

~%> zsh echo_dollard_9.zsh
/Users/yvon/work/zsh/echo_dollard_0.zsh

)))))
--
une bévue

MonkeeSage

10/1/2006 4:50:00 AM

0

Hi Une,

Une bévue wrote:
> in ruby what's the meaning of "$" before base ?
> a way to get it as global var ?

Yes, that's correct. So $0 is actually global variable named "0".

> quit frankly i was wrong i've believe the behaviour of shell scripts is
> different then, i've made a riny test in zsh (my prefered shell) :

Well, you were right and wrong. ;) If you run that script (or a ruby
script) from the directory where it lives at, you get just the filename
in $0, since the base path is '.'; but cd .. and run it, and then
you'll get a full path. Confusing? ;)

Regards,
Jordan

pere.noel

10/1/2006 5:33:00 AM

0

MonkeeSage <MonkeeSage@gmail.com> wrote:

> Well, you were right and wrong. ;) If you run that script (or a ruby
> script) from the directory where it lives at, you get just the filename
> in $0, since the base path is '.'; but cd .. and run it, and then
> you'll get a full path. Confusing? ;)

not at all.

a question apart from that (but linked to)

my script "direct.rb" lies in ~/bin/rb

when using it i do :

require '/Users/yvon/bin/rb/direct.rb'

or :

require "ENV['HOME']/bin/rb/direct.rb"

which isn't "direct" )))

my ENV['HOME']/bin is in my PATH

i know also their is a LOAD_PATH within Ruby.

then, the question :

what kind of var i've to setup in order to be able to write :

require 'direct'

and avoiding warnings of rubygems ???

notice i don't want my "~/bin/rb" being in the PATH...
--
une bévue

MonkeeSage

10/1/2006 6:25:00 AM

0

Une bévue wrote:
> what kind of var i've to setup in order to be able to write :
>
> require 'direct'
>
> and avoiding warnings of rubygems ???
>
> notice i don't want my "~/bin/rb" being in the PATH...

Two ways:

1) Copy direct.rb in the ruby lib / site_lib directory. You can see
where they are like this:

require 'rbconfig'
puts Config::CONFIG['rubylibdir']
puts Config::CONFIG['sitelibdir']

It is conventional (and easier to maintain) to put user scripts into
site_lib.

OR

2) Add something like this to every script where you want to require
direct.rb:

# $: is an alias for $LOAD_PATH
$: << '/some/dir' unless $:.include?('/some/dir')
require 'direct'

HTH,
Jordan

MonkeeSage

10/1/2006 6:29:00 AM

0


MonkeeSage wrote:
> # $: is an alias for $LOAD_PATH

Ps. Changing $LOAD_PATH does not effect ENV['PATH'] at all (either from
inside ruby or from the shell).

pere.noel

10/1/2006 8:52:00 AM

0

MonkeeSage <MonkeeSage@gmail.com> wrote:

> Two ways:
>
> 1) Copy direct.rb in the ruby lib / site_lib directory. You can see
> where they are like this:
>
> require 'rbconfig'
> puts Config::CONFIG['rubylibdir']
> puts Config::CONFIG['sitelibdir']
>
> It is conventional (and easier to maintain) to put user scripts into
> site_lib.
>
> OR
>
> 2) Add something like this to every script where you want to require
> direct.rb:
>
> # $: is an alias for $LOAD_PATH
> $: << '/some/dir' unless $:.include?('/some/dir')
> require 'direct'


may be their is a third way (using symlinks) ?

not working at the time being it seems ruby isn't following symlinks

i did a :

~%> sudo mkdir /opt/local/lib/ruby/site_ruby/1.8/yt

and then :

~%> sudo ln -s direct.rb /opt/local/lib/ruby/site_ruby/1.8/yt/direct

testing this :

--- direct_test.rb -----------------------------------------------------
require 'yt/direct'

puts "$basename = #{$basename}" ### line 3 ###
------------------------------------------------------------------------

i get :

> direct_test.rb
/opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require': no such file to load -- yt/direct (LoadError)
from
/opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require'
from /Users/yvon/bin/direct_test.rb:3
~/bin/rb%>


the path is correct because using :

--- ruby_libs_dir.rb ---------------------------------------------------
#!/usr/bin/env ruby -w

require 'rbconfig'
puts Config::CONFIG['rubylibdir']
puts Config::CONFIG['sitelibdir']
------------------------------------------------------------------------

i get :

~b%> ruby_libs_dir.rb
/opt/local/lib/ruby/1.8
/opt/local/lib/ruby/site_ruby/1.8

then i must conclude ruby isn't following symlinks (?) then no third
solution.

the reason, for me, using this kind of solution it's because i have two
installed ruby (apart from the one installed by default by Apple) :

one in /opt/local... (darwinports)
another installed in my HOME fo JRuby

then , i'd like avoiding copying scripts in different locations.

--
une bévue