Forum: Ruby directory name issue

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.
D10ca8d11301c2f4993ac2279ce4b930?d=identicon&s=25 verynewbie (Guest)
on 2006-06-09 00:29
here is the code

dirArray = Dir.entries(".")
Dir.mkdir "SMITH, JOHN X755055"
dirArray.each do
        fileentry = dirArray.pop
        if fileentry.match("txt")
                textfile = File.open(fileentry)
                textfile.each_line {|line|
                        newdir = line.upcase.to_s
                        if !File.exist?(newdir)
                                Dir.mkdir "#{newdir}"
                                print "Created directory: " + newdir +
"\n"
                        else
                                print "Directory " + newdir + " already
exists..
.\n"
                        end
                }
        end
end

i have a text file in the same directory
it has two lines, they look like this:

SMITH, ADAM X755077
SMITH, ADAM X755078

when I run the above script, the first Dir.mkdir works fine
however when it reads the first line of the text file, the
Dir.mkdir "#{newdir}" doesn't create a directory

i get this error

mkdirs.rb:9:in `mkdir': Invalid argument - "SMITH, ADAM X755077"
(Errno::EIN
VAL)
        from mkdirs.rb:9
        from mkdirs.rb:6:in `each_line'
        from mkdirs.rb:6
        from mkdirs.rb:2:in `each'
        from mkdirs.rb:2

what am I doing wrong
890028bf839dd5e0005612d44d0bbab1?d=identicon&s=25 Harry (Guest)
on 2006-06-09 02:51
(Received via mailing list)
>                        newdir = line.upcase.to_s

newdir = line.chomp.upcase.to_s

Your line has a linefeed. Chomp it.

Harry
890028bf839dd5e0005612d44d0bbab1?d=identicon&s=25 Harry (Guest)
on 2006-06-09 03:04
(Received via mailing list)
You can see it like this.


p line
p line.chomp


Harry
D10ca8d11301c2f4993ac2279ce4b930?d=identicon&s=25 verynewbie (Guest)
on 2006-06-09 03:46
Harry wrote:
>>                        newdir = line.upcase.to_s
>
> newdir = line.chomp.upcase.to_s
>
> Your line has a linefeed. Chomp it.
>
> Harry

thanks!

i should have thought of that part of it though
This topic is locked and can not be replied to.