Rails-like features in C# 3

I recently purchased a copy of Agile Web Development with Rails in an attempt to learn more about Ruby on Rails. I've been slowly working through the sample application and so far working with Rails has been a very pleasant experience.

One of the thing I really like about RoR is the very readable method names. For example, compare Ruby on Rails "Times" method to a C# "For" loop:

// C#
for(int i = 0; i < 10; i++) {

# Rails
10.times do |i|
  puts i

I know which one I prefer.

Just for fun, I decided to write a number of Rails-like methods using C# Extension Methods. Here are some examples:

//Rails style "For" loop
3.Times(i => Console.WriteLine(i));

//Rails style "Foreach" loop
List<int> ints = new List { 1, 2, 3 };
ints.Each(i => Console.WriteLine(i));

//Working with dates and times...
DateTime tenDaysAgo = 10.Days().Ago();
DateTime threeHoursInTheFuture = 3.Hours().FromNow();
threeHoursInTheFuture = 3.Hours().Since(DateTime.Now);

//String manipulation
string test = "My Test String";
string first = test.First(); //"M"
string firstThree = test.First(3); //"My "
string last = test.Last(); //"g"
string LastThree = test.Last(3); //"ing"
Written on December 4, 2007