Forum: Ruby Path management in ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0bf205083975ef054b4ad08915d1d653?d=identicon&s=25 zimba.tm (Guest)
on 2006-03-06 10:43
(Received via mailing list)
Hi list,

While playing and coding with ruby, I found it often useful to have a
better
way to handle paths that in the actual version. I think it would be nice
to
have an unified way to manage paths.

$:.class => Array
ENV['PATH'] => String

<code>
class Path < Array
  def initialize(path = nil)
    if path.kind_of? String
      paths = path.split(':')
      paths.each do |p|
        push p
      end
    elsif path.kind_of? Array
      path.each do |p|
        push p
      end
    end
  end

  def find(filename, ext=nil)
    each do |path|
      filepath = File.join(path, filename)
      return filepath if File.exist?( filepath )
      if ext
        filepath = File.join(path, filename + ext)
        return filepath if File.exist?( filepath )
      end
    end
    return nil
  end
  alias :include? find

  # TODO : The join symbol changes between the platforms. Make this
dependent
of the platform.
  def to_s
    join(':')
  end

end

p = Path.new($:)
p.include? 'ipaddr.rb'
=> "/usr/lib/ruby/1.8/ipaddr.rb"
</code>

--
Cheers,
  zimba.tm

weblog : http://zimba.oree.ch
5224d56434a3d237408c217605df8a56?d=identicon&s=25 Payton Swick (Guest)
on 2006-03-07 15:16
(Received via mailing list)
Have you seen the Pathname and Pathname2 modules?  They're part of the
standard library.  They are also platform independant and give you all
the features of File, FileUtils, and more.

-Payton
This topic is locked and can not be replied to.