Forum: Ruby Help getting file names from directorys on windows.

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.
Harry N. (Guest)
on 2009-01-18 17:41
I am new to code writting, have to learn Ruby (windows) so I am trying
to
do some experaments working with files and directories.

This code, (I thinks) creates an array called file. I can do some tricks
to test that, array.empty? and I get back false for each element. I want
to be able to select each element on at a time But all I get is numbers.

Dir.chdir("e:/sourcemp3")
puts Dir.pwd

Dir.foreach(".") do |file|
    puts file
end

The code above will list all files in the directory. I need only the
first
file with .mp3 extion. It would be better not to use the .foreach but
what
could I use?
David M. (Guest)
on 2009-01-18 18:34
Harry N. wrote:
> I am new to code writting, have to learn Ruby (windows) so I am trying
> to
> do some experaments working with files and directories.
>
> This code, (I thinks) creates an array called file. I can do some tricks
> to test that, array.empty? and I get back false for each element. I want
> to be able to select each element on at a time But all I get is numbers.
>
> Dir.chdir("e:/sourcemp3")
> puts Dir.pwd
>
> Dir.foreach(".") do |file|
>     puts file
> end
>
> The code above will list all files in the directory. I need only the
> first
> file with .mp3 extion. It would be better not to use the .foreach but
> what
> could I use?

I usually use the Dir.glob() method in such instances:

  puts Dir.glob('*.mp3').first

I hope that helps.

David
http://rubyonwindows.blogspot.com
This topic is locked and can not be replied to.