John Lam 1997 — 2026

RubyCLR Interfaces

A notable feature that was missing from the second drop of RubyCLR was support for interfaces. Also, I was missing support for integrating .NET Reflection with Ruby Reflection.

I spent some time yesterday adding these features to RubyCLR. This is what I wound up with:

def test_enumerator
  a = ArrayList.new
  a.Add(1)
  a.Add(2)
  a.Add(3)
  enum = a.get_interface('System.Collections.IEnumerable')
  e    = enum.GetEnumerator.get_interface('System.Collections.IEnumerator')
  assert e.MoveNext
  assert 1, e.Current
  assert e.MoveNext
  assert 2, e.Current
  assert e.MoveNext
  assert 3, e.Current
  assert !e.MoveNext
end

As you could imagine, I’m going to add explicit support for an each method on enumerable objects.

Implementing get_interface turned out to be easier than expected:

def get_interface(name)
  klass = self.class
  create_safe_ruby_instance_method(klass, 'get_interface') do
    ldstr        name.to_s
    ld_this      klass
    call         'static Marshal::ToRubyObjectAsInterface(String, Object)'
    ret
  end
  get_interface(name)
end

The C++ ToRubyObjectAsInterface simply thunks to some existing code in the C++ part of the RubyCLR runtime.

5 archived reader comments

Thibaut Barrère (2006-02-23)

Hi John

I haven’t taken the time to dive into the bridge source to find this answer myself, but what is the reason to require the framework 2.0 instead of 1.1 ?

keep up with the good work

Thibaut

John Lam (2006-02-23)

I use a new feature of CLR 2.0 called DynamicMethods to generate shims to connect Ruby objects to CLR objects. This feature doesn’t exist in 1.1. I could have done some other hackery to make it happen, but it was so much cleaner to do it in 2.0.

Besides, by the time this thing becomes “real”, wouldn’t everyone have upgraded already? :)

Thibaut (2006-02-23)

thanks for the explanation, it’s crystal clear now.

I’m pretty sure this thing will become “real” before everyone is upgraded :)

do you plan on hosting the project somewhere ? (rubyforge, sourceforge?)

John Lam (2006-02-23)

Yes - by the time I get to Beta 1, this thing will be hosted somewhere, likely rubyforge. I just finished a major refactoring of the code layout in anticipation of the move. The plan is to do one more ZIP-style release, and then host Beta 1 on rubyforge.

Thibaut (2006/02/23)

thanks for the explanation, it’s crystal clear now. I’m pretty sure this thing will become "real" before everyone is upgraded :) do you plan on hosting the project somewhere ? (rubyforge, sourceforge?)