なかだです。 At Tue, 29 Jun 1999 14:07:15 +0900, matz / netlab.co.jp (Yukihiro Matsumoto) wrote: > まあ、移植性の話をするとRubyはANSI Cの範疇からは相当逸脱して > いますが(なんてったって未だにold K&Rだし)、POSIXに限ってもス > クリプトの存在するディレクトリを得る確実な方法はないように思 > います。argv[0]に必ず入ってるとは期待できないでしょうし。 ruby 自身のパスが argv[0] に必ず入ってることは期待できませんが、スクリ プトのパスは fopen() するときには必ず分かってると思うのですが。カレント からの相対パスか絶対パスかはともかく。 argv[1] にディレクトリ情報が全くない場合でも、そのために -S オプション があるわけですし。 でも、単にこうすれば済むような気もするです。:-) BEGIN { $:.unshift(File.dirname(File.expand_path($0))) } あるいは、symlink も考慮するなら、こんな風。 BEGIN { require "followlink" $:.unshift(File.dirname(File.followlink($0))) } -- /| / /| / 中田伸悦 / 鹿沼.栃木 _/ _/OBU. _/ _/OKADA <nobu.nokada / softhome.net>
class File =begin followlink.rb Follows symbolic links and returns expanded true path name. May cause exception Errno::ELOOP =description File.followlink(path, max) path: The path name to be followed symbolic links. max: Then maximum level of allowed symbolic link. Exceeding links cause exception. Possible values: Numeric: immidiate. true: infinite, but exception raises same path name appears. false, nil: never raise exception. Default value is 20(i.e. on many systems' default). =end def followlink(path, max = 20) path = expand_path(path) links = [] while symlink? path if case max when Numeric (max -= 1) < 0 when true links.include? path else max end raise Errno::ELOOP, "Too many levels of symbolic links - " + path end links << path if max == true path = expand_path(readlink(path), dirname(path)) end path end module_function :followlink end