<?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>spiros.blog() &#187; Ruby</title>
	<atom:link href="http://www.tzavellas.com/techblog/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tzavellas.com/techblog</link>
	<description>Spiros Tzavellas’s blog, mostly on software development and Java.</description>
	<lastBuildDate>Sun, 22 Aug 2010 20:25:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>C# Extension methods</title>
		<link>http://www.tzavellas.com/techblog/2007/06/02/c-extension-methods/</link>
		<comments>http://www.tzavellas.com/techblog/2007/06/02/c-extension-methods/#comments</comments>
		<pubDate>Sat, 02 Jun 2007 07:45:33 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/2007/06/02/c-extension-methods/</guid>
		<description><![CDATA[Scott Hanselman has a post about a user who doesn&#8217;t get what is cool about Ruby. In the post you can find the below example that compares Java and Ruby in terms of code readability. The Java code: new Date&#40;new Date&#40;&#41;.getTime&#40;&#41; - 20 * 60 * 1000&#41; The Ruby code: 20.minutes.ago The article was interesting [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.hanselman.com/blog/">Scott Hanselman</a> has a <a href="http://www.hanselman.com/blog/ProgrammerIntentOrWhatYoureNotGettingAboutRubyAndWhyItsTheTits.aspx">post</a> about a user who doesn&#8217;t get what is cool about <a href="http://www.ruby-lang.org/en/">Ruby</a>.</p>
<p>In the post you can find the below example that compares <a href="http://java.sun.com">Java</a> and <a href="http://www.ruby-lang.org/en/">Ruby</a> in terms of code readability.</p>
<p>The Java code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">20</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>The Ruby code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">20.<span style="color:#9900CC;">minutes</span>.<span style="color:#9900CC;">ago</span></pre></div></div>

<p>The article was interesting but what really caught my attention was some comments from C# programmers who showed how, in a few lines of code, you can mimic the Ruby syntax in C# using extension methods.</p>
<p>The new version of C#, shipped with the <a href="http://msdn2.microsoft.com/en-us/vstudio/aa700830.aspx">Orcas</a> release of Visual Studio, has a new feature called extension methods. Extension methods were added to the .Net languages to support <a href="http://msdn2.microsoft.com/en-us/netframework/aa904594.aspx">LINQ</a> and they provide a way to attach methods to classes without changing their code.</p>
<p>The below code is copied and modified from the comments of Ian Cooper in the  aforementioned <a href="http://www.hanselman.com/blog/ProgrammerIntentOrWhatYoureNotGettingAboutRubyAndWhyItsTheTits.aspx">blog post</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> TimeExtensions <span style="color: #000000;">&#123;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> TimeSpan Minutes<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> <span style="color: #FF0000;">int</span> numberOfMinutes<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> TimeSpan<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, numberOfMinutes, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> DateTime Ago<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> TimeSpan numberOfMinutes<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> DateTime.<span style="color: #0000FF;">Now</span>.<span style="color: #0000FF;">Subtract</span><span style="color: #000000;">&#40;</span>numberOfMinutes<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>In the above code we defined a class, called <code>TimeExtensions</code>, which contains two extension methods. Notice the <code>this</code> keyword on the left of the fist parameter (both methods take one parameter in the example code) of the methods. The <code>this</code> keyword tells the C# compiler that a method is an extension method. The parameter annotated with the <code>this</code> keyword is the object on which the extension method is called. If I am correct, the methods are not really added to the classes (you can&#8217;t find them via reflection), the compiler at-compile-time translates them into static method calls.</p>
<p>In our example the first method, called <code>Minutes</code>, will be added to the <code>int</code> class (probably via auto-boxing) and the second method, called <code>Ago</code>, will be added to the <code>TimeSpan</code> class.</p>
<p>Now, to use the extension methods that we defined above we have to import them using the <code>using TimeExtensions</code> statement. This is a really good thing since the usage of extension methods is always explicit. You have to import them to use them and if you don&#8217;t want to use them you don&#8217;t import them.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">TimeExtensions</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> HelloWorld <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> 20.<span style="color: #0000FF;">Minutes</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Ago</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><code>20.Minutes().Ago()</code> is pretty close to <code>20.minutes.ago</code> <img src='http://www.tzavellas.com/techblog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>For a nice tutorial about C# 3.0 extension methods see: <a href="http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx">New &#8220;Orcas&#8221; Language Feature: Extension Methods</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2007/06/02/c-extension-methods/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
