The first pass of the code for implementing CLR interfaces in Ruby is now working. Here’s a simple example of implementing IEnumerator in Ruby:
class RubyEnumerator
implements IEnumerator
def initialize(ruby_object)
@position = -1
@ruby_object = ruby_object
end
# NOTE: hack for properties - will be "current" when done
def get__current
@ruby_object[@position]
end
def move_next
@position += 1
@ruby_object.length != @position
end
def reset
@position = -1
end
end
Here’s the modification to the Ruby Array class to support IEnumerable:
class Array
implements IEnumerable
def get_enumerator
RubyEnumerator.new(self)
end
end
Here’s the set of unit tests that validates this behavior. Note the use of inlined C#:
class ImplicitDataBindingTests < TestCase
inline :csharp do |compiler|
compiler.compile <<-EOF
using System.Collections;
namespace RubyClr.Tests {
public class RubyArrayDataBindingTarget {
public static int[] EnumArray(IEnumerable obj) {
ArrayList al = new ArrayList();
foreach(int current in obj)
al.Add(current);
return (int[])al.ToArray(typeof(int));
}
}
}
EOF
end
def test_array_bind
numbers = [1, 2, 3, 4, 5]
result = RubyArrayDataBindingTarget.enum_array(numbers)
assert_equal 1, result[0]
assert_equal 5, result[4]
end
end