[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

using a variable in File.new or File.open

arose

6/7/2006 9:29:00 PM

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?

14 Answers

Bit Twiddler

6/7/2006 9:43:00 PM

0

"arose" <arosel@ercot.com> wrote in message
news:1149715756.407284.310620@i39g2000cwa.googlegroups.com...
>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")


arose

6/7/2006 9:50:00 PM

0

that was it, thanks a lot

Bit Twiddler wrote:
> "arose" <arosel@ercot.com> wrote in message
> news:1149715756.407284.310620@i39g2000cwa.googlegroups.com...
> >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")

arose

6/8/2006 8:33:00 PM

0

How would you work chomp! into the following

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


arose wrote:
> that was it, thanks a lot
>
> Bit Twiddler wrote:
> > "arose" <arosel@ercot.com> wrote in message
> > news:1149715756.407284.310620@i39g2000cwa.googlegroups.com...
> > >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")

Tim Hoolihan

6/8/2006 9:37:00 PM

0

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

But it's probably more graceful if you just do it like this:
File.open("Reader.txt") {|g|
while line = g.gets
File.new(line)
end
}

arose wrote:
> How would you work chomp! into the following
>
> g=File.open("Reader.txt","r")
> g.each_line {|line| File.new(line.dump)}
>
>
> arose wrote:
>> that was it, thanks a lot
>>
>> Bit Twiddler wrote:
>>> "arose" <arosel@ercot.com> wrote in message
>>> news:1149715756.407284.310620@i39g2000cwa.googlegroups.com...
>>>> 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")
>

arose

6/8/2006 10:25:00 PM

0

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"


Tim Hoolihan wrote:
> Like this:
> g=File.open("Reader.txt","r")
> g.each_line {|line| File.new((line.dump).chomp)}
>
> But it's probably more graceful if you just do it like this:
> File.open("Reader.txt") {|g|
> while line = g.gets
> File.new(line)
> end
> }
>
> arose wrote:
> > How would you work chomp! into the following
> >
> > g=File.open("Reader.txt","r")
> > g.each_line {|line| File.new(line.dump)}
> >
> >
> > arose wrote:
> >> that was it, thanks a lot
> >>
> >> Bit Twiddler wrote:
> >>> "arose" <arosel@ercot.com> wrote in message
> >>> news:1149715756.407284.310620@i39g2000cwa.googlegroups.com...
> >>>> 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")
> >

arose

6/8/2006 10:32:00 PM

0

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?


arose wrote:
> 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"
>
>
> Tim Hoolihan wrote:
> > Like this:
> > g=File.open("Reader.txt","r")
> > g.each_line {|line| File.new((line.dump).chomp)}
> >
> > But it's probably more graceful if you just do it like this:
> > File.open("Reader.txt") {|g|
> > while line = g.gets
> > File.new(line)
> > end
> > }
> >
> > arose wrote:
> > > How would you work chomp! into the following
> > >
> > > g=File.open("Reader.txt","r")
> > > g.each_line {|line| File.new(line.dump)}
> > >
> > >
> > > arose wrote:
> > >> that was it, thanks a lot
> > >>
> > >> Bit Twiddler wrote:
> > >>> "arose" <arosel@ercot.com> wrote in message
> > >>> news:1149715756.407284.310620@i39g2000cwa.googlegroups.com...
> > >>>> 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")
> > >

arose

6/8/2006 10:35:00 PM

0

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?



arose wrote:
> 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"
>
>
> Tim Hoolihan wrote:
> > Like this:
> > g=File.open("Reader.txt","r")
> > g.each_line {|line| File.new((line.dump).chomp)}
> >
> > But it's probably more graceful if you just do it like this:
> > File.open("Reader.txt") {|g|
> > while line = g.gets
> > File.new(line)
> > end
> > }
> >
> > arose wrote:
> > > How would you work chomp! into the following
> > >
> > > g=File.open("Reader.txt","r")
> > > g.each_line {|line| File.new(line.dump)}
> > >
> > >
> > > arose wrote:
> > >> that was it, thanks a lot
> > >>
> > >> Bit Twiddler wrote:
> > >>> "arose" <arosel@ercot.com> wrote in message
> > >>> news:1149715756.407284.310620@i39g2000cwa.googlegroups.com...
> > >>>> 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")
> > >

Tim Hunter

6/8/2006 10:39:00 PM

0

arose wrote:
> 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.
>

File.new makes a new instance of the File class, not a new file. By
default that new File object is associated with a file that is open for
reading and thus must already exist.

If you want File.new to, as part of creating a new instance of the File
class, open a file for writing you have to give it an extra argument:

f = File.new("myfile", "w")

If "myfile" already exists then it will be erased. If you want to keep
the existing contents of "myfile" use "w+" instead of just "w".

Use ri to learn about File.new:

ri File.new

arose

6/8/2006 10:51:00 PM

0

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.

Tim Hunter wrote:
> arose wrote:
> > 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.
> >
>
> File.new makes a new instance of the File class, not a new file. By
> default that new File object is associated with a file that is open for
> reading and thus must already exist.
>
> If you want File.new to, as part of creating a new instance of the File
> class, open a file for writing you have to give it an extra argument:
>
> f = File.new("myfile", "w")
>
> If "myfile" already exists then it will be erased. If you want to keep
> the existing contents of "myfile" use "w+" instead of just "w".
>
> Use ri to learn about File.new:
>
> ri File.new

Tim Hunter

6/8/2006 11:06:00 PM

0

arose wrote:
> 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.

The File class gets the open method from its parent class, IO. Try

ri IO.open

Glad your program is working!