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

December 4. 2011 11:58 PM

Facebook news

Hi I like your post it is very informative.Thank you

Facebook news

December 4. 2011 11:59 PM

Gossip

I need to start learning .Net framework code. This can be very helpful. Thanks for sharing.

Gossip

December 5. 2011 12:00 AM

Gossip

I love programming, right now I astudy C+ for preoject smartphone application.

Gossip

December 5. 2011 12:01 AM

Gossip

I love Apple products!

Gossip

December 5. 2011 12:02 AM

Gossip

Hi all this is a nice post! I can help you all to get more Twitter followers for your profile.

Gossip

December 5. 2011 12:03 AM

Gossip

I made a lot of traffic with facebook, and FB fan pages. I will suggest you to try this technique.

Gossip

December 5. 2011 12:04 AM

Gossip

HI I appreciate this post, but I also love RubyonRails.

Gossip

January 13. 2012 01:40 PM

zagam2

Pingback from  A set of articles on Ruby « QuantuMatrix’s Weblog
<a href="http://howtounblockfacebook.net">How To Unblock Facebook</a>

zagam2

January 16. 2012 09:41 PM

seo

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

seo

January 19. 2012 08:45 AM

Hochzeitsfotograf Zuerich

like to override the default behavior of a .net class, how can I declare a generic class..
<a href="www.weddingphotographers.ch/.../">Hochzeitsfotograf Zuerich</a>

Hochzeitsfotograf Zuerich

January 21. 2012 10:00 PM

here

I need to start learning .Net framework code. This can be very helpful. Thanks for sharing.

here

January 23. 2012 09:14 PM

www.bestsamedaypaydayloans.co.uk

This is really a very cool and fun article. I really love to travel and I really do think this is very helpful.

www.bestsamedaypaydayloans.co.uk

February 3. 2012 12:54 PM

pay day loan online

methods that can be used when it comes to butter stains, but the important here is to tackle the stain as soon as possible. ..

pay day loan online

February 3. 2012 02:40 PM

cigarette sales

Use those who theoretic can handwork things aright in the unheeding secernment...

cigarette sales

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