Forum: IronRuby Calling overloaded methods

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
E1e2dd8c03c8e9e0680cfd802df0a04b?d=identicon&s=25 Alex 2k8 (alex_2k8)
on 2009-01-09 17:16

    public class Cls {
        public void foo(object a) {
            Console.WriteLine("foo #1");
        public void foo(object[] a) {
            Console.WriteLine("foo #2");

Ruby:[3, 4])

    foo #1
    foo #1  <-- Calling foo(object a) again

So how can I call foo(object[] a)?

- Alex
F983f0c990cba2fe743ef62a975ec99c?d=identicon&s=25 Curt Hagenlocher (Guest)
on 2009-01-09 18:34
(Received via mailing list)
You'll need to build a CLR array rather than a Ruby array.

require 'mscorlib'
System::Array.create_instance(System::Object.to_clr_type, 2)
o = System::Array.create_instance(System::Object.to_clr_type, 2)
>>> o[0] = 3
>>> o[1] = 4

You can monkey-patch Array and add this as a helper:

class Array
  def to_clr_ary
    o = System::Array.create_instance(System::Object.to_clr_type,
    each_with_index { |obj, i| o[i] = obj }

CLR arrays and Ruby arrays are totally different; Ruby arrays are much
more similar to "List<object>" than to "object[]".  So it's unlikely
that IronRuby would ever do this conversion automatically for you.
Cb51033949ffccd982ae32c9f890f25a?d=identicon&s=25 Tomas Matousek (Guest)
on 2009-01-09 18:41
(Received via mailing list)
[3,4] is not an array of objects. It's a RubyArray.

You can create a CLR array like this:

v = System::Array.of(Object).new(3)
[1,2,3].each_with_index { |x,i| v[i] = x }

E1e2dd8c03c8e9e0680cfd802df0a04b?d=identicon&s=25 Alex 2k8 (alex_2k8)
on 2009-01-09 21:12
Thank you!
This topic is locked and can not be replied to.