# Dir.entries, output showing nils

**URL:** <https://www.ruby-forum.com/t/dir-entries-output-showing-nils/172590>\
**Category:** Ruby\
**Created:** [July 23, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/dir-entries-output-showing-nils/172590 "2009-07-23T17:42:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [July 23, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/dir-entries-output-showing-nils/172590/1 "2009-07-23T17:42:44Z")

</div>

When I output to line for RegisteredUser it shows all filenames as nil  
for all but the files. What can I change to show only the filename  
meeting the conditions?  
Thanks, MC

outputs…  
nil  
nil  
nil  
nil  
AUR123443.txt

Dir.entries(ascpath).each do |filename|

if filename =~ /^AU/ then  
if filename[2…2] == “R” then  
RegisteredUser = filename  
else  
NonRegisteredUser = filename  
end  
end

#below shows nil for all files and then shows the registereduser file.  
puts RegisteredUser

end

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [July 23, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/dir-entries-output-showing-nils/172590/2 "2009-07-23T18:27:03Z")

</div>

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
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

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?

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [July 23, 2009, 6:36pm UTC](https://www.ruby-forum.com/t/dir-entries-output-showing-nils/172590/3 "2009-07-23T18:36:54Z")

</div>

Rob B. 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
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> 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

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [July 23, 2009, 6:35pm UTC](https://www.ruby-forum.com/t/dir-entries-output-showing-nils/172590/4 "2009-07-23T18:35:50Z")

</div>

On Fri, Jul 24, 2009 at 01:23:27AM +0900, Rob B. wrote:

> > nil  
> > end
> 
> (which is Enumerable#partition)
> 
> Does that help?
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

He’s also defining it inside the ‘each’ which, I believe, localizes the  
variable. Thus,  
it will be nil at the beginning of each loop. If not set (i.e. the file  
does not match), it will  
be nil, as he is saying now.

Seems to me you want something more like this:

if filename =~ /^AUR/ then  
puts filename  
end

It should also be noted that you do not need to say [2…2] if you want  
the 3rd char of a string, just [2]  
will do.

HTH,

- Kyle

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [July 23, 2009, 6:44pm UTC](https://www.ruby-forum.com/t/dir-entries-output-showing-nils/172590/5 "2009-07-23T18:44:44Z")

</div>

> It should also be noted that you do not need to say [2…2] if you want  
> the 3rd char of a string, just [2]  
> will do.

when I puts filename[2] it returns the 56, or 48 which I think is the  
r’s character id or maybe even index for that string. Not sure…

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [July 23, 2009, 9:48pm UTC](https://www.ruby-forum.com/t/dir-entries-output-showing-nils/172590/6 "2009-07-23T21:48:48Z")

</div>

On Jul 23, 2009, at 12:45 PM, Mmcolli00 Mom wrote:

> > It should also be noted that you do not need to say [2…2] if you  
> > want  
> > the 3rd char of a string, just [2]  
> > will do.
> 
> when I puts filename[2] it returns the 56, or 48 which I think is the  
> r’s character id or maybe even index for that string. Not sure…

In Ruby 1.8, String#[] with a single Integer argument returns the  
character, which as you’ve found is the ASCII code for the character.

In Ruby 1.9, String#[] returns a 1-character string (and you can apply  
String#ord to it if you want the value)

So filename[2…2] will give you a 1-character string in both versions.  
I’d stick with it.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [July 23, 2009, 10:09pm UTC](https://www.ruby-forum.com/t/dir-entries-output-showing-nils/172590/7 "2009-07-23T22:09:50Z")

</div>

On Jul 23, 2009, at 12:37 PM, Mmcolli00 Mom wrote:

> > First, Capitalized names in Ruby are constants. You probably want a  
> > files.partition {|filename| filename[2…2] == “R”}  
> > PUR233\_somename.txt…
> 
> 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
