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.


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

October 20. 2010 08:49 PM

razvan dimescu

I would like to override the default behavior of a .net class, how can I declare a generic class in ruby?

razvan dimescu

January 9. 2011 03:32 AM

pingback

Pingback from robfortner82.wordpress.com

CES 2011 Camera Preview « Rob Fortner's Blog

robfortner82.wordpress.com

March 14. 2011 10:03 AM

pingback

Pingback from radanhepzi.myblogvoice.com

Fastest Way to Make Money – 4 Things You Can Surely Do-
Radan

radanhepzi.myblogvoice.com

July 24. 2011 01:40 PM

pingback

Pingback from tefevoger.wordpress.com

Stocks add to gains on budget deal reports
    (Reuters) | tefevoger

tefevoger.wordpress.com

July 25. 2011 09:30 AM

pingback

Pingback from bie1.elexitolatino.com

Tito loves Dana: MMA?s favorite marriage continues, what?s next for Ortiz? | MMA Blog

bie1.elexitolatino.com

July 30. 2011 10:08 AM

pingback

Pingback from itvnewslocal.laozx.net

Book Review: Physics of the Future, by Michio Kaku | SevenPonds BlogItv News Local | Itv News Local

itvnewslocal.laozx.net

August 7. 2011 03:50 AM

pingback

Pingback from wpcube.com

Bike Courier Services – All That You Need to Know

wpcube.com

September 29. 2011 07:11 PM

pingback

Pingback from seoline.it

Lista di Blog Pr1-6 a Gratis | SeoLine - La Tua Linea Seo

seoline.it

November 19. 2011 06:17 AM

pingback

Pingback from friendsonlinefullepisodes.karatekidwallpapers.com

The oldest old: Reaching 90 more likely than ever (San Jose Mercury News) | Friends Online Full Episodes

friendsonlinefullepisodes.karatekidwallpapers.com

April 15. 2012 10:46 AM

pingback

Pingback from glennbeckrushlimbaugh.jonas-poland.com

Google named official social platform of 2012 Republican National Convention | Glenn Beck Rush Limbaugh

glennbeckrushlimbaugh.jonas-poland.com

April 15. 2012 10:46 AM

pingback

Pingback from astronomyarticles.frshnk.biz

Nike Fuelband Review: A Friendly Reminder to Stop Being Lazy [Fitmodo] | Astronomy Articles

astronomyarticles.frshnk.biz

February 11. 2013 01:04 PM

online account software

I am glad to sharing your thing, let me know so much about your information .

online account software

March 7. 2013 01:05 AM

Symbol (Motorola) MC5590 Handheld wireless barcode scanner

Hello I wish to to share a comment here concerning you to definitely be able to inform you just how much i personally Loved this particular study. I have to elope in order to aTurkey Day time Supper but desired to leave ya an easy comment. We preserved you Same goes with be returning subsequent function to read more of yer quality articles. Keep up the quality work.

Symbol (Motorola) MC5590 Handheld wireless barcode scanner

April 2. 2013 01:30 PM

professional mobile website designer

This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the good work.

professional mobile website designer

April 2. 2013 01:37 PM

professional mobile website designer

This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the good work.

professional mobile website designer

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



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