Forum: Ruby append static text for all files in a directory

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.
Bcc578efe3bb69e763ec7695d675c06a?d=identicon&s=25 Srinath A. (srinathbtech)
on 2009-02-24 07:17
How to append static text for all files in application .
I want to append comment lines on top of each file in app/controllers/

how can iterate and append in the app ?

I used this for single file :

filename = File.open("application.rb","a") do |f|
f.puts "append to file";
f.close();
end



thanks !!
7caba80fca2ac4ddbb7f55957e5c0dec?d=identicon&s=25 Jun Young Kim (Guest)
on 2009-02-24 07:45
(Received via mailing list)
If your code is normally executed, the following code will give you
some hint.

Dir.glob("*.rb") do |file|
    puts file
end

2009. 02. 24, ¿ÀÈÄ 3:16, Srinath A. ÀÛ¼º:
Bcc578efe3bb69e763ec7695d675c06a?d=identicon&s=25 Srinath A. (srinathbtech)
on 2009-02-24 07:51
Hi jun,

thanks for reply.

how can we append text on top of each file in all .rb files through
iterating ??

thanks !

Jun Young Kim wrote:
> If your code is normally executed, the following code will give you
> some hint.
>
> Dir.glob("*.rb") do |file|
>     puts file
> end
>
> 2009. 02. 24, ���� 3:16, Srinath A. �ۼ�:
1d53b088a989e069b94597c282eebbbc?d=identicon&s=25 Simon Krahnke (Guest)
on 2009-02-24 09:07
(Received via mailing list)
* Srinath A. <srinathbtech@gmail.com> (07:16) schrieb:

> How to append static text for all files in application .
> I want to append comment lines on top of each file in app/controllers/

Appending is "inserting" text at the end of the file. If you want to
modify something before the end you got to read it all in, do your
changes, and write it out again.

If your Files are small enough to be read in whole, it's quite easy in
Ruby:

content = File.read(filename)
File.open(filename, "w") do | f |
  f << "# comment\n"
  f << content
end

mfg,                     simon .... untested
Bcc578efe3bb69e763ec7695d675c06a?d=identicon&s=25 Srinath A. (srinathbtech)
on 2009-02-25 06:36
The code below was working fine , but deleting the first line in the
files and writing the added one :

Dir.glob("application.rb") do |file|
    File.open(file,"r+") do |f|
    puts file
   f.puts "Commenting is very common style.\nCommenting helps more easy
readability"
end
end

how can we prepend text in file with out deleting the existing content
??

thanks !!



Simon Krahnke wrote:
> * Srinath A. <srinathbtech@gmail.com> (07:16) schrieb:
>
>> How to append static text for all files in application .
>> I want to append comment lines on top of each file in app/controllers/
>
> Appending is "inserting" text at the end of the file. If you want to
> modify something before the end you got to read it all in, do your
> changes, and write it out again.
>
> If your Files are small enough to be read in whole, it's quite easy in
> Ruby:
>
> content = File.read(filename)
> File.open(filename, "w") do | f |
>   f << "# comment\n"
>   f << content
> end
>
> mfg,                     simon .... untested
This topic is locked and can not be replied to.