<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vidul Nikolaev Petrov &#187; file</title>
	<atom:link href="http://www.vidul.com/category/ruby/file/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vidul.com</link>
	<description>my web space</description>
	<lastBuildDate>Sat, 24 Sep 2011 06:11:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Simple File Manipulation</title>
		<link>http://www.vidul.com/2008/07/21/simple-file-manipulations/</link>
		<comments>http://www.vidul.com/2008/07/21/simple-file-manipulations/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 23:15:00 +0000</pubDate>
		<dc:creator>Vidul</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[file]]></category>

		<guid isPermaLink="false">urn:uuid:{a.guid}</guid>
		<description><![CDATA[Ruby file manipulation is as easy as:

MyFile.create &#34;test.txt&#34;
MyFile.add &#34;test.txt&#34;, &#34;some content&#34;
MyFile.remove &#34;test.txt&#34;

For example create a file caled fileeasy.rb and add the following module to it:

module FileEasy
&#160;
  def self.included&#40;subject&#41;
    subject.extend&#40;ClassMethods&#41;
  end
&#160;
  module ClassMethods
   alias_method :delete, :remove
   alias_method :unlink, :remove
&#160;
    def create&#40;filename&#41;
   [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby file manipulation is as easy as:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:arial,verdana,monospace;">MyFile.<span style="color:#9900CC;">create</span> <span style="color:#996600;">&quot;test.txt&quot;</span>
MyFile.<span style="color:#9900CC;">add</span> <span style="color:#996600;">&quot;test.txt&quot;</span>, <span style="color:#996600;">&quot;some content&quot;</span>
MyFile.<span style="color:#9900CC;">remove</span> <span style="color:#996600;">&quot;test.txt&quot;</span></pre></div></div>

<p>For example create a file caled fileeasy.rb and add the following module to it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:arial,verdana,monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> FileEasy
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span><span style="color:#006600; font-weight:bold;">&#40;</span>subject<span style="color:#006600; font-weight:bold;">&#41;</span>
    subject.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>ClassMethods<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">module</span> ClassMethods
   alias_method <span style="color:#ff3333; font-weight:bold;">:delete</span>, <span style="color:#ff3333; font-weight:bold;">:remove</span>
   alias_method <span style="color:#ff3333; font-weight:bold;">:unlink</span>, <span style="color:#ff3333; font-weight:bold;">:remove</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> create<span style="color:#006600; font-weight:bold;">&#40;</span>filename<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>filename, <span style="color:#996600;">&quot;w&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> add<span style="color:#006600; font-weight:bold;">&#40;</span>filename, text<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>filename, <span style="color:#996600;">&quot;a&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> f <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">&lt;</span> text <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> remove<span style="color:#006600; font-weight:bold;">&#40;</span>filename<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>filename<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Then test it with irb:
</pre>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:arial,verdana,monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'fileeasy'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> MyClass
  <span style="color:#9966CC; font-weight:bold;">include</span> FileEasy
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
MyFile.<span style="color:#9900CC;">create</span> <span style="color:#996600;">&quot;text.txt&quot;</span>
MyFile.<span style="color:#9900CC;">add</span> <span style="color:#996600;">&quot;text.txt&quot;</span>, <span style="color:#996600;">&quot;test content&quot;</span>
MyFile.<span style="color:#9900CC;">remove</span> <span style="color:#996600;">&quot;text.txt&quot;</span></pre></div></div>

<p>Of course this post is just a simple tutorial and the standard library is the best choice – <a href="http://www.ruby-doc.org/stdlib/libdoc/fileutils/rdoc/index.html">FileUtils</a>.</p>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-center">
<ul class="socials">
		<li class="sexy-blinklist">
			<a href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http://www.vidul.com/2008/07/21/simple-file-manipulations/&amp;Title=Simple+File+Manipulation" rel="nofollow" title="Share this on Blinklist">Share this on Blinklist</a>
		</li>
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://www.vidul.com/2008/07/21/simple-file-manipulations/&amp;title=Simple+File+Manipulation" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.vidul.com/2008/07/21/simple-file-manipulations/&amp;title=Simple+File+Manipulation" rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-reddit">
			<a href="http://reddit.com/submit?url=http://www.vidul.com/2008/07/21/simple-file-manipulations/&amp;title=Simple+File+Manipulation" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.vidul.com/2008/07/21/simple-file-manipulations/&amp;title=Simple+File+Manipulation" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://www.vidul.com/2008/07/21/simple-file-manipulations/" rel="nofollow" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=Simple+File+Manipulation+-+http://b2l.me/bmwtr+" rel="nofollow" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="sexy-newsvine">
			<a href="http://www.newsvine.com/_tools/seed&amp;save?u=http://www.vidul.com/2008/07/21/simple-file-manipulations/&amp;h=Simple+File+Manipulation" rel="nofollow" title="Seed this on Newsvine">Seed this on Newsvine</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

]]></content:encoded>
			<wfw:commentRss>http://www.vidul.com/2008/07/21/simple-file-manipulations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

