append static text for all files in a directory

S

Srinath A.

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 !!
 
J

Jun Young Kim

If your code is normally executed, the following code will give you =20
some hint.

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

2009. 02. 24, =BF=C0=C8=C4 3:16, Srinath A. =C0=DB=BC=BA:
 
S

Srinath A.

Hi jun,

thanks for reply.

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

thanks !
 
S

Simon Krahnke

* Srinath A. said:
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
 
S

Srinath A.

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 !!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top