# Path management in ruby

**URL:** <https://www.ruby-forum.com/t/path-management-in-ruby/77231>\
**Category:** Ruby\
**Created:** [March 6, 2006, 10:43am UTC](https://www.ruby-forum.com/t/path-management-in-ruby/77231 "2006-03-06T10:43:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zimba.tm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/aeb1de/32.png) [@zimba.tm](https://www.ruby-forum.com/u/zimba.tm)\
**Post date:** [March 6, 2006, 10:43am UTC](https://www.ruby-forum.com/t/path-management-in-ruby/77231/1 "2006-03-06T10:43:04Z")

</div>

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

`
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”
`

–  
Cheers,  
zimba.tm

weblog : [http://zimba.oree.ch](http://zimba.oree.ch)

---

<div class="post-metadata">

**Author:** ![zimba.tm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/aeb1de/32.png) [@zimba.tm](https://www.ruby-forum.com/u/zimba.tm)\
**Post date:** [March 7, 2006, 3:16pm UTC](https://www.ruby-forum.com/t/path-management-in-ruby/77231/2 "2006-03-07T15:16:24Z")

</div>

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
