Writing to a file without opening it


#1

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?


#2

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


#3

On 6/6/06, Yoann G. removed_email_address@domain.invalid wrote:

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

That worked like a charm, thanks!


#4

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


#5

On 6/5/06, removed_email_address@domain.invalid removed_email_address@domain.invalid 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. =)