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
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
Try __FILE__
On 9/30/06, Une bévue <pere.noel@laponie.com.invalid> wrote:
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
Michael Guterl wrote:
Try __FILE__
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
In the executed script, __FILE__ will contain the same as $0. Cf the "if
$0 == __FILE__ main() end" idiom.
What's wrong with using basename again?
David Vallner
On 9/30/06, Une bévue <pere.noel@laponie.com.invalid> wrote:
Michael Guterl wrote:
> Try __FILE__
>
>>
>> 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
>>In the executed script, __FILE__ will contain the same as $0. Cf the "if
$0 == __FILE__ main() end" idiom.
Doh. Silly me, I was running it from . so no path was being added...
What's wrong with using basename again?
On 9/30/06, David Vallner <david@vallner.net> wrote:
> On 9/30/06, Une bévue <pere.noel@laponie.com.invalid> wrote:
David Vallner
Michael Guterl
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)....
David Vallner <david@vallner.net> wrote:
--
une bévue
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
yes ! i knew File.basenamepath)...
MonkeeSage <MonkeeSage@gmail.com> wrote:
I think this is pretty direct: File.basename($0) ... perhaps you meant
immediate?
--
une bévue
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
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
)))))
MonkeeSage <MonkeeSage@gmail.com> wrote:
--
une bévue
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
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...
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?
--
une bévue
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 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).
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 ###
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'
------------------------------------------------------------------------
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
Une bévue wrote:
~%> 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
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 )))
MonkeeSage <MonkeeSage@gmail.com> wrote:
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
--
une bévue
Une bévue wrote:
NOPE :[
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
fine, thanks a lot , that works prety well !
MonkeeSage <MonkeeSage@gmail.com> wrote:
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
--
une bévue