getting the name of the script in use

U

Une bévue

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 ?)
 
D

David Vallner

--------------enig2D723A8A4DDFCD867F16FEAF
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Michael said:
Try __FILE__
=20

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

What's wrong with using basename again?

David Vallner


--------------enig2D723A8A4DDFCD867F16FEAF
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFHraGy6MhrS8astoRAhYXAJ9VROgiLHQgHNJ4fnx0usr/hoCbhQCdFBCq
v/heuBoPyo7kJsZo1Ouxyhg=
=NCql
-----END PGP SIGNATURE-----

--------------enig2D723A8A4DDFCD867F16FEAF--
 
U

Une bévue

David Vallner said:
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)....
 
M

MonkeeSage

Une said:
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
 
M

MonkeeSage

Une said:
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
 
U

Une bévue

MonkeeSage said:
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

)))))
 
M

MonkeeSage

Hi Une,
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
 
U

Une bévue

MonkeeSage said:
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...
 
M

MonkeeSage

Une said:
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
 
M

MonkeeSage

MonkeeSage said:
# $: 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).
 
U

Une bévue

MonkeeSage said:
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.
 
M

MonkeeSage

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

Try adding a .rb to the symlink:

sudo rm -f /opt/local/lib/ruby/site_ruby/1.8/yt/direct
sudo ln -s direct.rb /opt/local/lib/ruby/site_ruby/1.8/yt/direct.rb

Regards,
Jordan
 
U

Une bévue

MonkeeSage said:
Try adding a .rb to the symlink:

sudo rm -f /opt/local/lib/ruby/site_ruby/1.8/yt/direct
sudo ln -s direct.rb /opt/local/lib/ruby/site_ruby/1.8/yt/direct.rb

NOPE :[

i even try putting an alias file (from folder /Users/yvon/bin/rb) named
"yt" into "/opt/local/lib/ruby/site_ruby/1.8" but this too don't work...

i'll stay to your first way solution and do another cp for JRuby )))
 
M

MonkeeSage

Une said:

OK, you need to do an absolute symlink and it should work:

sudo rm -f /opt/local/lib/ruby/site_ruby/1.8/yt/direct.rb
sudo ln -s /FULL/PATH/TO/direct.rb
/opt/local/lib/ruby/site_ruby/1.8/yt/direct.rb

Regards,
Jordan
 
U

Une bévue

MonkeeSage said:
OK, you need to do an absolute symlink and it should work:

sudo rm -f /opt/local/lib/ruby/site_ruby/1.8/yt/direct.rb
sudo ln -s /FULL/PATH/TO/direct.rb
/opt/local/lib/ruby/site_ruby/1.8/yt/direct.rb

fine, thanks a lot , that works prety well !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,214
Messages
2,571,112
Members
47,704
Latest member
DavidSuita

Latest Threads

Top