From: Rob Biedenharn Date: 2009-07-24T05:00:38+09:00 Subject: Re: Dir.entries, output showing nils On Jul 23, 2009, at 12:37 PM, Mmcolli00 Mom wrote: > Rob Biedenharn wrote: >> On Jul 23, 2009, at 11:42 AM, Mmcolli00 Mom wrote: >>> AUR123443.txt >>> >>> #below shows nil for all files and then shows the registereduser >>> file. >>> puts RegisteredUser >>> >>> end >> >> First, Capitalized names in Ruby are constants. You probably want a >> local_variable instead. >> >> I'm not sure from your example and question what you really want, but >> here are some options: >> >> # An array of filenames that start with AU >> files = Dir.glob("AU*") # or Dir["AU*"] >> >> # Split one array into a pair of arrays (in another array) based on a >> condition: >> files.partition {|filename| filename[2..2] == "R"} >> >> (which is Enumerable#partition) >> >> Does that help? >> > > My project consists of me reading the contents of a folder. The folder > will have 5 files for testing. The files are named as such... > > AUR234_somename.txt > PUR233_somename.txt.. > > If a file starts with AU I know that this file fits the Aero group and > the third letter being 'R' tells me if it is registered. If a file > fits > the condition. I want to store that filename so that other programs > can > peform I/O tests on it. The 5 files will be replaced occasionally but > with the same strict naming conventions. Each file will be meant for a > different type of test. The number represent a count that will be > validated later in the test as well. > > Good tip- I will apply lowercase letters to the filenames. I don't > think > the partition will help in this case though. Thanks -MC Ah, much better description of your problem. Dir["directory/*"] will include the "directory/" part in the returned name. (didn't I see this in your code? anyway...) Dir["[Aa][Uu][Rr]*"] gives you exactly these files You could also: Dir["[Aa][Uu]*"].partition {|file| file =~ /\A..r/i } to get files where the third character is R or r. In a Regexp, /\A/ is the beginning of the string, /./ is any character, and the /r/i is an r matched insensitive to case (that's the i flag at the end). -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com