Introducing ActiveSupport.NET

posted @ Thursday, February 07, 2008 3:18 PM

 

To my surprise, none of my googling seemed to come up with anyone doing this.  So I figured I'd start it off (please let me know if you know of any other efforts already underway).  As some would say, taking extension method abuse to a whole new level.  :D  But, I see these little utility methods to be a good fit as extensions methods to make the syntactic sugar that much sweeter.

One of the many great things about working in Rails is the core extensions in the ActiveSupport library.  It just *becomes* part of the Ruby language as you work with Rails.  And I wanted to get that same feeling when working in C#.  So this is my attempt to start porting over the core extensions from Ruby's ActiveSupport Library.

So far I've just ported over a few string access and conversion extensions, but check 'em out and let me know if you'd find something like this to be useful.  Here are some examples of what's currently supported...

 1: // some accessors
 2: "blah".At(2) // 'a'
 3: "blah".First() // 'b'
 4: "blah".First(2) // "bl"
 5: "blah".From(1) // "lah"
 6: "blah".Last() // 'h'
 7: "blah".Last(2) // "ah"
 8: "blah".To(2) // "bla"
 9:  
 10: // couple simple conversions (still needs lots of work)
 11: "1/4/2008".ToDate() // DateTime
 12: "01:15:35".ToTime() // DateTime

You can grab the source at its google code repository.  You can just do a "build test" from a command line in the trunk to run the unit test suite.

Enjoy.  :-)

Comments
Jeremy D. Miller - 2/7/2008 3:26 PM
# re: Introducing ActiveSupport.NET
Thumbs up from me. I'm thinking about how much repetitive string manipulation I've done in my lifetime that could have been avoided with some of this.
Joey Beninghove - 2/7/2008 3:30 PM
# re: Introducing ActiveSupport.NET
Cool. Yeah, I'm especially looking forward to extending ints to be date/time-aware and such. 6.days.ago, 3.minutes.FromNow(), etc....
Wil Bloodworth - 2/7/2008 3:40 PM
# re: Introducing ActiveSupport.NET
Joey,

I'm just going to play devil's advocate... for the heck of it. What's wrong with the following equivalents that already exist? With the exception of the two that I commented on below, I think the others are easy to use... although may not be as readable as your extension methods.

"blah"[2]
"blah"[0]
"blah".Substring(0, 2)
"blah".Substring(1)
"blah"[3] // Not exactly equivalent to .Last. Could do "blah".Substring("blah".length - 1)
"blah".Substring("blah".Length - 2, 2) // Not as nice as .Last(2)
"blah".Substring(0, 3)
DateTime.Parse("1/4/2008")
DateTime.Parse("01:15:35")

BTW, I did a little ditty on extension methods and multiple inheritance trickery on my blog.

- Wil
Joey Beninghove - 2/7/2008 3:48 PM
# re: Introducing ActiveSupport.NET
Yeah, I knew this would get asked. Sure, some of these simple string accessors might not be a clear advantage all the time. But I think these same ideas would really help for ints, dates and some of the other base types. Plus, you get chaining, which can sometimes really improve readability.

Think of this... which is more clear...

DateTime.Parse("1/1/2004").Month
OR
"1/1/2004".Month() // not implemented yet, but certainly could be

To me the 2nd one has much less "noise" and helps readability quite a bit. But of course no one is required to use them. :)
Jimmy Bogard - 2/7/2008 4:09 PM
# re: Introducing ActiveSupport.NET
I got blasted for my 5.Times().Do(() => Console.WriteLine("Jimmy is kewl")); so I don't see how this can fly.

http://grabbagoft.blogspot.com/2007/10/ruby-style-loops-in-c-30.html

I'm going to check around, but there might be a similar effort to this out there too...
Pedro Martínez Juliá - 2/8/2008 1:38 PM
# re: Introducing ActiveSupport.NET
Take a look to Mono.Rocks. It is a (little by now) collection of useful extension methods.
Joey Beninghove - 2/8/2008 1:43 PM
# re: Introducing ActiveSupport.NET
@Pedro,
Ahh, very cool. I'll definitely check it out.
Joey Beninghove - 2/9/2008 10:04 PM
# re: Introducing ActiveSupport.NET
@Marco,
I just did some basic perf tests doing increasing numbers of passes (1mil, 10mil, 100mil) on your specific example and performance hits don't even start to creep in until you cross the 1mil mark. Now this is just a very rough test that I threw together, but here are the outputs I got from the different number of passes...

1mil: 0-15ms difference
10mil: 47-62ms difference
100mil: 516-532ms difference
Post Comment






Please add 2 and 6 and type the answer here: