[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Newbie Question About files

Jeff Singer

5/1/2005 2:18:00 PM

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.



7 Answers

ts

5/1/2005 2:24:00 PM

0

>>>>> "J" == Jeff Singer <jsinger@freshlooktech.com> writes:

J> 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

5/1/2005 2:27:00 PM

0

Pit Capitain

5/1/2005 2:30:00 PM

0

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

5/1/2005 2:36:00 PM

0

Robert Klemme

5/2/2005 7:23:00 AM

0


"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} }

:-)

Kind regards

robert

Ara.T.Howard

5/2/2005 1:53:00 PM

0

Ara.T.Howard

5/2/2005 3:00:00 PM

0