Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   append static text for all files in a directory (http://www.velocityreviews.com/forums/t856053-append-static-text-for-all-files-in-a-directory.html)

Srinath A. 02-24-2009 06:16 AM

append static text for all files in a directory
 
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 !!
--
Posted via http://www.ruby-forum.com/.


Jun Young Kim 02-24-2009 06:43 AM

Re: append static text for all files in a directory
 
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:

> 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 =3D File.open("application.rb","a") do |f|
> f.puts "append to file";
> f.close();
> end
>
>
>
> thanks !!
> --=20
> Posted via http://www.ruby-forum.com/.
>
>







Srinath A. 02-24-2009 06:50 AM

Re: append static text for all files in a directory
 
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. �ۼ�:


--
Posted via http://www.ruby-forum.com/.


Simon Krahnke 02-24-2009 07:53 AM

Re: append static text for all files in a directory
 
* 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

Srinath A. 02-25-2009 05:35 AM

Re: append static text for all files in a directory
 
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


--
Posted via http://www.ruby-forum.com/.



All times are GMT. The time now is 11:48 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57