Forum: Ruby iterating over directories

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.
66b2639b3ceb68d53e7ce0327d38caf6?d=identicon&s=25 Ruby-er Rubyist-maximus (dafydd)
on 2009-04-15 07:41
I got this oneliner that isn't producing the output I was expecting (it
printing all of the directories in the linux root directory).

irb(main):012:0> Dir.new("/").each { |name| puts name if
File.directory?(name) }
..
.
=> #<Dir:/>

Does anyone know what I might be doing incorrectly?
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-04-15 07:59
(Received via mailing list)
On Wed, Apr 15, 2009 at 7:41 AM, Dafydd Fontenot
<fontenot.1031@gmail.com> wrote:
> I got this oneliner that isn't producing the output I was expecting (it
> printing all of the directories in the linux root directory).
>
> irb(main):012:0> Dir.new("/").each { |name| puts name if
> File.directory?(name) }
> ..
> .
> => #<Dir:/>
>
> Does anyone know what I might be doing incorrectly?

It should be printing all of the directories in the linux root.
What do you want it to do?

Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
66b2639b3ceb68d53e7ce0327d38caf6?d=identicon&s=25 Ruby-er Rubyist-maximus (dafydd)
on 2009-04-15 08:04
Andrew Timberlake wrote:
> On Wed, Apr 15, 2009 at 7:41 AM, Dafydd Fontenot
> <fontenot.1031@gmail.com> wrote:
>
> It should be printing all of the directories in the linux root.
> What do you want it to do?

I'm just having it print for now for debugging purposes. But eventually
it will be storing all of the directory names in an array. Or is there
any other way I can control that block to do different things if the
current item is a file and do something else if it is a directory?
60ddadf1a8a8ebac760e7e4cc1e342b3?d=identicon&s=25 Siddick Ebramsha (siddick)
on 2009-04-15 08:04
Solution :-
 Dir.new("/").each { |name| puts name if File.directory?( "/" + name) }
66b2639b3ceb68d53e7ce0327d38caf6?d=identicon&s=25 Ruby-er Rubyist-maximus (dafydd)
on 2009-04-15 08:24
Siddick Ebramsha wrote:
> Solution :-
>  Dir.new("/").each { |name| puts name if File.directory?( "/" + name) }

Solved, thank you good sir.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-15 09:31
(Received via mailing list)
2009/4/15 Dafydd Fontenot <fontenot.1031@gmail.com>:
> Siddick Ebramsha wrote:
>> Solution :-
>>  Dir.new("/").each { |name| puts name if File.directory?( "/" + name) }
>
> Solved, thank you good sir.

Here's another one:

puts Dir["/*"].select {|f| test ?d, f}

Cheers

robert
This topic is locked and can not be replied to.