Forum: Ruby Itterating through one's path(s)

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.
508861510639b56d61eb30e6a28b01d9?d=identicon&s=25 John Maclean (Guest)
on 2006-02-12 03:07
(Received via mailing list)
I'd like to see if I can itterate through the directories
of my PATH (or $PATH for Unix-based systems) using blocks.

#!/usr/bin/ruby -w path =
ENV['PATH'].split(File::PATH_SEPARATOR)

path.each{|zz| Dir.new zz}

There's an example of using nested for loops in
file:///usr/share/doc/ruby-1.8.4/sample/dir.rb
....
# directory access
# list all files but .*/*~/*.o
dirp = Dir.open(".")
for f in dirp
  case f
  when /^\./, /~$/, /\.o/
    # do not print
  else
    print f, "\n"
  end
end
dirp.close
Ee5c1f36549c4ddca2189f9c4cf36f2c?d=identicon&s=25 Scott (Guest)
on 2006-02-12 05:23
(Received via mailing list)
I'm not exactly sure what your're trying to accomplish.  If you're
trying to get a list of all the files that are under each path in the
ENV['PATH'] variable, methinks this'll work:

files = []
filter = /^\./, /~$/, /\.o/ # btw, this regex is invalid.
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
  files.concat Dir.open(path).select { |d| !d.match(filter) }
end

If thats totally not what you're trying to do, then maybe you could
clarify?  Also, the regex you list isnt valid, the forward slashes need
to be escaped, but even then, there wont be any forward slashes in your
file names.

Scott
508861510639b56d61eb30e6a28b01d9?d=identicon&s=25 John Maclean (Guest)
on 2006-02-12 14:02
(Received via mailing list)
On Sun, 12 Feb 2006 13:23:24 +0900
"Scott" <bauer.mail@gmail.com> wrote:

> If thats totally not what you're trying to do, then maybe you could
> clarify?  Also, the regex you list isnt valid, the forward slashes
> need to be escaped, but even then, there wont be any forward slashes
> in your file names.
>
> Scott
>
>
>
>


--
John Maclean
MSc (DIC)
07739 171 531

Thanks for the reply and tip. You've guessed correctly that all I'm
wanting to do is to list all of the files in my $PATH. I included the
listing of /usr/.../dir.rb just to show that there's some "test" source
code on my system and that it uses a nested for loop.
This topic is locked and can not be replied to.