John Lam 1997 — 2026

Enumerations in RubyCLR

I added enumerations support to RubyCLR today. It was a bit tricky since Ruby has no native support for enumerations (or user-defined value types), so I have to fake out enumerations using Ruby objects. For most scenarios, it feels very much like an enumeration would in C#.

Here’s a simple Windows Forms MessageBox sample that uses enumerations:

require 'rubyclr'
RubyClr::reference 'System.Windows.Forms'
include System::Windows::Forms
result = MessageBox.Show('Yes or no?', 'Answer this!',
MessageBoxButtons::YesNo)
puts result.to_s

The to_s method outputs the string value of the enum, in this case Yes or No based on which button the user clicks. I also support adding, subtracting and bitwise or operations against enumerations.

My overload resolution algorithm needs to account for implicit up-conversions to the enumeration’s underlying type. That will be accounted for when I rewrite that algorithm beginning tomorrow.

2 archived reader comments

Justin (2006/02/15)

John, I finally got .NET 2.0 installed. Any chance you have some new bits I can try out? Looks like you’ve made a lot of progress since the last time you posted them. p.s. Best wishes to your new baby :)

John Lam (2006/02/15)

Thanks, Justin! I just ran into a nasty problem while thinking about implementing nested types (I need to do this now for even the simplest Windows Forms app since Form.Control is now an instance of nested type Form+ControlCollection). I’m hopeful to get something out this week. I hate nested types.