<?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>Fri, 24 Sep 2010 07:42:49 +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(new Date().getTime() - 20 * 60 * 1000) 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>
<pre>new Date(new Date().getTime() - 20 * 60 * 1000)</pre>
<p>The Ruby code:</p>
<pre>20.minutes.ago</pre>
<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>
<pre>
public static class TimeExtensions {

  public static TimeSpan Minutes(this int numberOfMinutes) {
    return new TimeSpan(0, numberOfMinutes, 0);
  }

  public static DateTime Ago(this TimeSpan numberOfMinutes) {
    return DateTime.Now.Subtract(numberOfMinutes);
  }
}
</pre>
<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>
<pre>
using System;
using TimeExtensions;

public class HelloWorld {
  public static void Main() {
    Console.WriteLine( 20.Minutes().Ago() );
  }
}
</pre>
<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>

