Starter Step

C#….the ruby way

Posted on: September 1, 2009

If you have ever chatted with me about programming languages, its terribly obvious that I love ruby. Its just plain fun to write ruby code. I wouldn’t call myself a ruby nazi, but I would say that I get excited when I can do ruby like things in other languages. I have been working with C# for about two months getting TripCase on the Windows Mobile platform. My appreciation for c# is actually growing more and more. We use lambdas quite frequently in our Windows Mobile implementation which makes our code look alot like ruby.

Just yesterday I wanted to titleize a piece of text. My goal would be to just call:

   "jake loves ruby".Titleize();

Unfortunately the method wasn’t available….until I wrote this:

public static class StringExtensions
    { 
        public static string Titleize(this string text)
        {
          CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text)
        }
    }

Now I can call my Titleize method as if it were a method on an instance of the String class. Cool huh….yeah I thought so. You can read more about extension methods (thats what the c# kids are calling it) here

Update:
While this is cool, I could not figure out how to take an already existing method on a class and extend its behavior. In ruby we can do this with our method alias trick….but still..props to c# for extensions.

Advertisements

1 Response to "C#….the ruby way"

I can tell you’re a ruby guy because you forgot the ‘return’ keyword. Thanks for the tip though!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


  • Dave: I can tell you're a ruby guy because you forgot the 'return' keyword. Thanks for the tip though!
  • Chandrashekhar H M: Hi, Thanks its working fine in iOS 6 but not in iOS 7.0. Any Suggestion on this.
  • Coeur: To change a rootViewController, without all this TVNavigationController : myNewRoot = [[UIViewController alloc] init]; myNavigationController.view
%d bloggers like this: