IronRuby Tip: Using Generic CLR Methods from IronRuby

A while ago I wrote a post about using Generic CLR classes from IronRuby. This time I’ll share with you its less intuitive friend – using generic CLR methods from IronRuby.

As a result of Ruby being a duck-typed language which works with types implicitly, generics is not really needed. The whole language is generic… This is why using generic CLR types might become a bit odd for the language. Nonetheless, this fact will not stop us. The price to pay in order to enjoy Ruby in the .Net framework is worth it! :)

Anyway, assuming we have the following C# class defined in file sample.dll:

namespace ClassLibrary1
{
    public class Class1
    {
        public string Test<T>(T param)
        {
                return param.ToString();
        }
    }
}

In order to use it with a specific type, you need to do two things – get the generic method object (by using the method method) and provide it with the type via the special of method. For example, the next code invokes Test with a string and pass it “Shay Friedman”:

require "sample.dll"

sample = ClassLibrary1::Class1.new
sample.method(:test).of(String).call("Shay Friedman") # returns "Shay Friedman"


And that’s all there is to know.

All the best,
Shay.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Comments

September 8. 2010 06:54 PM

homes for rent by owner

I have to say, as much as I loved reading what you needed to say, I couldnt help however lose curiosity after a while. Its as if you had a fantastic grasp on the subject matter, however you forgot to incorporate your readers. Maybe you should take into consideration this from multiple angle. Or maybe you shouldnt generalise so much. Its better if you concentrate on what others may must say instead of just going for a intestine response to the subject. Think about adjusting your personal thought process and giving others who might read this the advantage of the doubt.

homes for rent by owner

Add comment




  Country flag

biuquote
  • Comment
  • Preview



Subscribe Subscribe

That's Me!

Hi! I'm Shay Friedman
I'm Shay Friedman - a Visual C#/IronRuby MVP, a consultant and instructor of .NET technologies, author, speaker and new technologies freak
More about me

Contact Me

> Contact page
> Twitter: @ironshay
> LinkedIn profile

Search

Hosted By

Sponsers