Tip: How To Use Generic .Net Classes in IronRuby

In the latest version of IronRuby (0.3), the ability to use Generic .Net classes was added. I couldn’t find anywhere how to do that so I dug it out of the code and now I’ll share it with you!

Example #1 - List

This is how to define an Int32 list, add two numbers to it and print them:

list = System::Collections::Generic::List[System::Int32].new

list.add 4
list.add 12

list.each { |x| puts x }

Of course we can also use Ruby type like Numeric, String, etc.

Example #2 – Dictionary

This is how to declare a number-string dictionary, add values to it and print them:

dict = System::Collections::Generic::Dictionary[Fixnum, String].new

dict.add 1, "Hey"
dict.add 15, "There"

dict.each { |x| puts "#{x.key} - #{x.value}" }

 

All the best,
Shay.


IronRuby 0.3 Has Been Released!

Great news! As Jimmy Schementi describes it – the “conference-driven development schedule” of IronRuby brings us some news for the MIX09 conference. It’s the 0.3 version of IronRuby!

IronRuby 0.3 is out!

So what’s new in 0.3?

  1. IronRuby now passes 10% more of the RubySpec than v0.2 has. Make sure to check IronRuby.info in order to get up-to-date statistics.
  2. Better .Net interoperability – you can now call generic methods and implement interfaces!
  3. More! you can read about all changes in the CHANGELOG.txt file that you’ll find in the zip.

Grab it: http://rubyforge.org/frs/download.php/53552/ironruby-0.3.0.zip

Enjoy,
Shay.


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

I'm hosting this site on Arvixe and I'm very happy with it.
If you're looking for ASP.NET hosting, I highly recommend it
(and if you order from this link I also get some beer money!)
Web Hosting