using a variable in File.new or File.open

A

arose

I am trying to make a list of filenames in a txt file and have those
files generated.

g=File.open("Reader.txt","r")
g.each_line {|line| File.new(line.dump)}

I get an error message "Invalid argument - "ThisIsMyFileName.txt" which
is the first name in the Reader.txt file
and it points to the second line of my code above.

I try something similar like this

puts "What do you want to call your text file?"
newFileName=gets
g=File.open(newFileName,"w")
g.close()

I get an error "invalid Argument - whatever i type in"

If i try this though it works.
newFileName="Imadeafile"
g=File.open(newFileName,"w")
g.close()

I'm super new to Ruby, in fact this is my first program. What concept
am I missing here?
 
B

Bit Twiddler

arose said:
I am trying to make a list of filenames in a txt file and have those
files generated.

g=File.open("Reader.txt","r")
g.each_line {|line| File.new(line.dump)}

I get an error message "Invalid argument - "ThisIsMyFileName.txt" which
is the first name in the Reader.txt file
and it points to the second line of my code above.

I try something similar like this

puts "What do you want to call your text file?"
newFileName=gets
g=File.open(newFileName,"w")
g.close()

I get an error "invalid Argument - whatever i type in"

If i try this though it works.
newFileName="Imadeafile"
g=File.open(newFileName,"w")
g.close()

I'm super new to Ruby, in fact this is my first program. What concept
am I missing here?

Don't you have to do a String#chomp on the filename that you read in (from
the file or stdin)? This will remove the trailing newline character.

For example: g=File.open(newFileName.chomp!,"w")
 
A

arose

that was it, thanks a lot

Bit said:
Don't you have to do a String#chomp on the filename that you read in (from
the file or stdin)? This will remove the trailing newline character.

For example: g=File.open(newFileName.chomp!,"w")
 
A

arose

How would you work chomp! into the following

g=File.open("Reader.txt","r")
g.each_line {|line| File.new(line.dump)}
 
A

arose

man i have a long ways to go, neither of those seem to work

the first option gives me the error Invalid argument '
"ThisIsMyFileName" (the first row of Reader.txt)

the second one gives" me the error no such file or directory -
ThisIsMyFileName"
 
A

arose

Ok might have something to do with file.new if i do the following:

g=File.open("Reader.txt","r")
g.each_line {|line| File.new((line).chomp)}

I get the "no file name or directory" error. if i have line.dump there
seems to be double quotes around what is read from Reader.txt?!?!?!?

If I create a file with the name that is being read from Reader.txt the
program works fine. I have been assuming File.new makes a new file,
but it seems to want the file to allready exist. That is strange.

Any advice?
 
A

arose

Ok might have something to do with file.new if i do the following:

g=File.open("Reader.txt","r")
g.each_line {|line| File.new((line).chomp)}

I get the "no file name or directory" error. if i have line.dump there
seems to be double quotes around what is read from Reader.txt?!?!?!?

This works:
g=File.open("Reader.txt","r")
g.each_line {|line| File.open((line).chomp, "w")}

I have been assuming File.new makes a new file and File.open,
opens one that exists. That is strange.

Any advice?
 
A

arose

Ok that makes sense. Thanks a lot. Check this: ri File.open returns
"nothing is known about File.open", yet it works in my code.

ri File.new DOES give me information on File.new

both File.new and File.open work in my *awesome* program.
 
A

arose

thanks again



Tim said:
In addition, if you're not sure what parent class method comes from,
just enter the method:

ri open

It will list the classes that it has that method from, and you can make
an intelligent guess where your class is inheriting it from:

ri IO#open
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top