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.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Comments

October 5. 2009 05:17 PM

trackback

IronRuby Tip: Using Generic CLR Methods from IronRuby

IronRuby Tip: Using Generic CLR Methods from IronRuby

IronShay

December 2. 2009 04:09 AM

pingback

Pingback from quantumatrix.wordpress.com

A set of articles on Ruby « QuantuMatrix’s Weblog

quantumatrix.wordpress.com

Add comment




  Country flag

biuquote
  • Comment
  • Preview



Subscribe Subscribe

That's Me!

Hi! I'm Shay Friedman
I'm Shay Friedman - an eager developer, author, speaker and new technologies freak
More about me

Contact Me

> Contact page
> Twitter: @ironshay
> LinkedIn profile

Search

Sponsers