From: Robert Klemme Date: 2005-05-02T16:24:25+09:00 Subject: Re: Newbie Question About files "Jeff Singer" 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