views:

37

answers:

1

It seems impossible to create an object using its default constructor when there is a static .New() method defined on the class:

.NET class:

public class Tester
{
    public static void New()
    {
        Console.WriteLine("In Tester.New()");
    }

    public Tester()
    {
        Console.WriteLine("In constructor");
    }
}

IronRuby code:

Tester.new
Tester.New

Both of these lines call Tester.New(), not the constuctor. It seems impossible to call the constructor of the Tester class.

Is there a workaround, or is this a bug?

+1  A: 

The first one is just an unavoidable ambiguity. If you want to make CLI classes look like Ruby classes, you have no choice but to map the constructor to a new method. So, if you have both a real new method and a synthesized one which maps to a constructor, whatever you do, either the synthetic method shadows the real one or the other way around. Either way, you lose.

That's why all CLI classes have a synthetic clr_new method:

Tester.clr_new
# In constructor
Jörg W Mittag
It doesn't really do a translation in the second case. It does call the static New() method, as expected
Philippe Leybaert
@Philippe Leybaert: Yes, of course. I don't know why I got confused there. If it *were* doing a translation it would obviously call the constructor.
Jörg W Mittag