Forum: Ruby Grade my test?

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.
7070c0ca59124b4d96dd27ea52ad359e?d=identicon&s=25 jabowen (Guest)
on 2007-06-16 07:12
(Received via mailing list)
I have directory of data files along with a few other types of files.
The
data file names start at 0000 and run to 3999.  I have a program that
creates an array for the data files without the non data files.  This
program works good but have I created the shortest way of doing the
task?
Thanks for grading my test.  Jeff

#Puts the contents of the current directory into data_files.
dir_file_list = Dir.entries(".")
data_files = []

dir_file_list.each do|files|
  if files.to_i > 0 && files.to_i < 4000
    data_files.push files
  end
end

puts data_files
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2007-06-16 11:48
(Received via mailing list)
On 6/16/07, jabowen <ja_bowen@yahoo.com> wrote:
>
> dir_file_list.each do|files|
>         if files.to_i > 0 && files.to_i < 4000
>                 data_files.push files
>         end
> end
>
> puts data_files

That would work just fine.  Here's another one:

data_files = Dir.entries.select { |file| (1...4000).include? file.to_i }

Be aware of how #to_i works, though.  This will grab a file named
"24-7Project", for example.

Todd
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-06-16 12:11
(Received via mailing list)
On 6/16/07, jabowen <ja_bowen@yahoo.com> wrote:
>
> I have directory of data files along with a few other types of files.  The
> data file names start at 0000 and run to 3999.  I have a program that
> creates an array for the data files without the non data files.  This
> program works good but have I created the shortest way of doing the task?
> Thanks for grading my test.  Jeff
>
> #Puts the contents of the current directory into data_files.
> dir_file_list = Dir.entries(".")
> data_files = []
   >
> dir_file_list.each do|files|
>         if files.to_i > 0 && files.to_i < 4000
>                 data_files.push files
>         end
> end
>
> puts data_files

Does it really work? What about file "0000' ?
The problem is that "a".to_i --> 0
I would go for

   Dir["[0-9]"*4]

or
   Dir["[0-3]" + "[0-9]"*3]
if necessary.

HTH
Robert
This topic is locked and can not be replied to.