From: Matthew Smillie Date: 2006-06-23T03:39:36+09:00 Subject: Re: Why is "nil" being included in an array? On Jun 22, 2006, at 19:22, Peter Bailey wrote: > I'm trying to simply lower-case all of the files in a directory. It > doesn't work because RUBY complains about not being able to convert > "nil" to a string. Why in the world is it even including "nil" in my > array? > > files = Dir.glob('*.pdf') > files.each do |file| > File.rename(file, file.downcase!) > end An excellent illustration of why side-effects can be harmful. file.downcase! has to be evaluated before File.rename can be evaluated. #downcase! returns nil if no changes are made, so for any file that's already in lower case, you're making this call: File.rename(file, nil) You can fix this if you change the ! method to the normal #downcase. This will also help you avoid a more subtle bug: files = Dir.glob('Test') => ["Test"] files.each do |file| File.rename(file, file.downcase!) end files = Dir.glob('Test') => ["Test"] # What the hell? shouldn't that be gone? It's the same bug biting you in a different way: this time, because #downcase! is evaluated before the call to #rename, it's modifying the file variable in-place, so #rename is effectively being called like this: File.rename('test', 'test') Where 'test' obviously doesn't exist in the first place (or even if it did, it's still not what you wanted). matthew smillie.