Newbie Question About files

I am still fairly new to ruby, and have been working on a fairly small project.
Ive looked through the online book on ruby central, Why's Poignant guide, and the ruby-doc documentation on both File and IO, but I still can't get the program to work.
Im trying ouput an array containing binary data to a file, the code is:

176: filename = gets
177: aFile = File.new(filename, 'w')
178: @file.each {|element| aFile.print element}
179: aFile.close

@file is an array of bytes, broken into blocks of 500 that Id like to send to the file that Im prompted for.
And those numbers aren't actually there, as they are the line numbers.

The error I get no matter how I format the name of the file is:
project.rb:177:in `initialize': Invalid argument - *whatever_I_just_typed* (Errno::EINVAL)

Does anyone have any Ideas/suggestions on how to get this to work.

Does anyone have any Ideas/suggestions on how to get this to work.

What is your OS, windows ?

Try

  filename = gets.chomp # remove \n at the end

Guy Decoux

Jeff Singer schrieb:

The error I get no matter how I format the name of the file is:
project.rb:177:in `initialize': Invalid argument - *whatever_I_just_typed* (Errno::EINVAL)

Hi Jeff,

"gets" returns the string you entered *including* the CR, try

   p filename

alter line 176. If you remove the trailing CR with

   filename = gets.chomp

it should work.

Regards,
Pit

"Jeff Singer" <jsinger@freshlooktech.com> schrieb im Newsbeitrag
news:4274E508.5020002@freshlooktech.com...

I am still fairly new to ruby, and have been working on a fairly small
project.
Ive looked through the online book on ruby central, Why's Poignant
guide, and the ruby-doc documentation on both File and IO, but I still
can't get the program to work.
Im trying ouput an array containing binary data to a file, the code is:

176: filename = gets
177: aFile = File.new(filename, 'w')
178: @file.each {|element| aFile.print element}
179: aFile.close

@file is an array of bytes, broken into blocks of 500 that Id like to
send to the file that Im prompted for.
And those numbers aren't actually there, as they are the line numbers.

The error I get no matter how I format the name of the file is:
project.rb:177:in `initialize': Invalid argument -
*whatever_I_just_typed* (Errno::EINVAL)

Does anyone have any Ideas/suggestions on how to get this to work.

Additional notes to make your code a bit safer:

- use binary output, regardless of OS, for documentation and portability

- use the block form of File.open, for exception safety

- use write instead of print, to avoid any interpretation of the data to
print

So, assuming that @file contains an array of strings with maxlen 500
(these hold the binary data I assume), that would make:

filename = gets.chomp
File.open( filename, 'wb' ) do |aFile|
  @file.each {|data| aFile.write data}
end

You can even make it a 1-liner:

File.open( gets.chomp, 'wb' ) {|aFile| @file.each {|data| aFile.write
data} }

:slight_smile:

Kind regards

    robert

guess you figured out the 'gets.chomp' bit (though i'd actually do gets.strip
since a leading space would hose you too).

assuming you meant 500 __bytes__ you can output the entire array at once
using:

   File::new(filename, 'wb'){|f| f.write(@file.join.pack)}

-a

···

On Sun, 1 May 2005, Jeff Singer wrote:

I am still fairly new to ruby, and have been working on a fairly small
project.
Ive looked through the online book on ruby central, Why's Poignant
guide, and the ruby-doc documentation on both File and IO, but I still
can't get the program to work.
Im trying ouput an array containing binary data to a file, the code is:

176: filename = gets
177: aFile = File.new(filename, 'w')
178: @file.each {|element| aFile.print element}
179: aFile.close

@file is an array of bytes, broken into blocks of 500 that Id like to
send to the file that Im prompted for.
And those numbers aren't actually there, as they are the line numbers.

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
renunciation is not getting rid of the things of this world, but accepting
that they pass away. --aitken roshi

===============================================================================

ts wrote:

> > > > > > "J" == Jeff Singer <jsinger@freshlooktech.com> writes:
> 
>  > Does anyone have any Ideas/suggestions on how to get this to work.
> What is your OS, windows ?
> Try
> filename = gets.chomp # remove \n at the end
> Guy Decoux

Yes, Im on windows.

Wouldn’t your code add binary data to a file (I already have the data),
instead of output it to a file?

oops - hit sent before this was done... meant

   File::new(filename,'wb'){|f| f.write @file.join}

sorry for noise.

-a

···

On Mon, 2 May 2005 Ara.T.Howard@noaa.gov wrote:

On Sun, 1 May 2005, Jeff Singer wrote:

I am still fairly new to ruby, and have been working on a fairly small
project.
Ive looked through the online book on ruby central, Why's Poignant
guide, and the ruby-doc documentation on both File and IO, but I still
can't get the program to work.
Im trying ouput an array containing binary data to a file, the code is:

176: filename = gets
177: aFile = File.new(filename, 'w')
178: @file.each {|element| aFile.print element}
179: aFile.close

@file is an array of bytes, broken into blocks of 500 that Id like to
send to the file that Im prompted for.
And those numbers aren't actually there, as they are the line numbers.

guess you figured out the 'gets.chomp' bit (though i'd actually do gets.strip
since a leading space would hose you too).

assuming you meant 500 __bytes__ you can output the entire array at once
using:

File::new(filename, 'wb'){|f| f.write(@file.join.pack)}

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
renunciation is not getting rid of the things of this world, but accepting
that they pass away. --aitken roshi

===============================================================================

Jeff Singer wrote:

ts wrote:

> > > > > > > "J" == Jeff Singer <jsinger@freshlooktech.com> writes:
> > 
> >  > Does anyone have any Ideas/suggestions on how to get this to work.
> > What is your OS, windows ?
> > Try
> > filename = gets.chomp # remove \n at the end
> > Guy Decoux

Yes, Im on windows.

Wouldn’t your code add binary data to a file (I already have the data),
instead of output it to a file?
Im sorry, I wasn’t even thinking, I just saw gets, and didn’t think
about where it would go, thank you.