Writing to a file without opening it

File.open(file, ‘a+’) won’t work when the file is not readable.

However, in a shell I can append a string using
echo string >> file

Am I forced to use exec + echo to append content, or is there a way to
append a string to a file using ruby code… without opening the file?

On Tue, 6 Jun 2006, Sy Ali wrote:

File.open(file, ‘a+’) won’t work when the file is not readable.

However, in a shell I can append a string using
echo string >> file

Am I forced to use exec + echo to append content, or is there a way to
append a string to a file using ruby code… without opening the file?

harp:~ > rm file

harp:~ > touch file

harp:~ > chmod 200 file

harp:~ > strace sh -c ‘echo string >> file’ 2>&1|grep open|tail -1
open(“file”, O_WRONLY|O_APPEND|O_CREAT|O_LARGEFILE, 0666) = 3

harp:~ > ruby -e’ open(“file”,
File::WRONLY|File::APPEND|File::CREAT, 0666){|f| f.puts “string2”} ’

harp:~ > chmod 600 file

harp:~ > cat file
string
string2

-a

On 6/6/06, Yoann G. [email protected] wrote:

Use mode ‘a’ instead of ‘a+’ in File.open

That worked like a charm, thanks!

File.open(file, ‘a+’) won’t work when the file is not readable.

However, in a shell I can append a string using
echo string >> file

Am I forced to use exec + echo to append content, or is there a way to
append a string to a file using ruby code… without opening the file?

Use mode ‘a’ instead of ‘a+’ in File.open

Yoann

On 6/5/06, [email protected] [email protected] wrote:

harp:~ > ruby -e’ open(“file”, File::WRONLY|File::APPEND|File::CREAT, 0666){|f| f.puts “string2”} ’

Just a quick thanks… this pointed me in the right direction for some
of the more advanced uses of ‘open’. It’ll come in handy. =)