Forum: Ruby Writing to a file without opening it

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sy Ali (Guest)
on 2006-06-06 08:24
(Received via mailing list)
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?
unknown (Guest)
on 2006-06-06 08:42
(Received via mailing list)
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
Yoann G. (Guest)
on 2006-06-06 14:26
(Received via mailing list)
> 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
Sy Ali (Guest)
on 2006-06-06 15:19
(Received via mailing list)
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!
Sy Ali (Guest)
on 2006-06-07 08:11
(Received via mailing list)
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.  =)
This topic is locked and can not be replied to.