# Calling constructor when static .New() method is defined

**URL:** <https://www.ruby-forum.com/t/calling-constructor-when-static-new-method-is-defined/190874>\
**Category:** IronRuby\
**Created:** [June 29, 2010, 7:02pm UTC](https://www.ruby-forum.com/t/calling-constructor-when-static-new-method-is-defined/190874 "2010-06-29T19:02:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Leybaert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/49beb7/32.png) [@Philippe\_Leybaert](https://www.ruby-forum.com/u/Philippe_Leybaert)\
**Post date:** [June 29, 2010, 7:02pm UTC](https://www.ruby-forum.com/t/calling-constructor-when-static-new-method-is-defined/190874/1 "2010-06-29T19:02:42Z")

</div>

Shay F. suggested I’d post this to the mailing list, as it may be  
a bug in IronRuby:

It seems impossible to call a constructor when there is a static .New()  
method defined on a class.

This is a minimal test case demonstrating the problem:

.NET (C#) 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

```

The ruby code above calls Tester.New() in both cases. There seems to be  
no way to call the regular constructor of this class.

---

<div class="post-metadata">

**Author:** ![Philippe\_Leybaert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/49beb7/32.png) [@Philippe\_Leybaert](https://www.ruby-forum.com/u/Philippe_Leybaert)\
**Post date:** [June 29, 2010, 7:29pm UTC](https://www.ruby-forum.com/t/calling-constructor-when-static-new-method-is-defined/190874/2 "2010-06-29T19:29:38Z")

</div>

You can call CLR constructor using “clr\_new” method:

Tester.clr\_new

Tomas

---

<div class="post-metadata">

**Author:** ![Philippe\_Leybaert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/49beb7/32.png) [@Philippe\_Leybaert](https://www.ruby-forum.com/u/Philippe_Leybaert)\
**Post date:** [June 29, 2010, 8:31pm UTC](https://www.ruby-forum.com/t/calling-constructor-when-static-new-method-is-defined/190874/3 "2010-06-29T20:31:42Z")

</div>

Thanks, that does seem to work.

But I personally think this behavior is completely wrong. Calling .new  
from IronRuby should always call the constructor, not a static method  
named .New() that happens to exist somewhere in the class hierarchy. (if  
the .New() method is defined in a base class, you get the same problem).

Philippe

---

<div class="post-metadata">

**Author:** ![Philippe\_Leybaert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/49beb7/32.png) [@Philippe\_Leybaert](https://www.ruby-forum.com/u/Philippe_Leybaert)\
**Post date:** [June 29, 2010, 9:15pm UTC](https://www.ruby-forum.com/t/calling-constructor-when-static-new-method-is-defined/190874/4 "2010-06-29T21:15:02Z")

</div>

Philippe Leybaert wrote:

> But I personally think this behavior is completely wrong. Calling .new from  
> IronRuby should always call the constructor, not a static method named  
> .New() that happens to exist somewhere in the class hierarchy. (if the  
> .New() method is defined in a base class, you get the same problem).

“new” is just a Ruby method on the Class object, which Ruby happens to  
call to construct an object. Because it’s just a method, it can be  
overridden. For example, a Ruby class’s #new can be overridden to return  
ANY object.

> > > class Foo  
> > > … def self.new  
> > > … 42  
> > > … end  
> > > … end  
> > > =\> nil  
> > > Foo.new  
> > > =\> 42  
> > > Foo.new.class  
> > > =\> Fixnum

So, anywhere in the class hierarchy, #new can be overridden. For  
example, if a base class overrides new, it does so for all its  
subclasses also, just as normal inheritance tells it do:

> > > class Bar \< Foo  
> > > … end  
> > > =\> nil  
> > > Bar.new  
> > > =\> 42

At a high-level, the way IronRuby’s .NET interop works is by treating  
any .NET-based code as if it were Ruby code, so a CLR Type will be shown  
as a Ruby Class. Therefore, defining a “New” method in C# is the moral  
equivalent of defining “new” on any Ruby class, which will override  
Class#new, which in turn overrides the CLR .ctor. Which is why “clr\_new”  
is provided.

~js

---

<div class="post-metadata">

**Author:** ![Philippe\_Leybaert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/49beb7/32.png) [@Philippe\_Leybaert](https://www.ruby-forum.com/u/Philippe_Leybaert)\
**Post date:** [June 29, 2010, 9:44pm UTC](https://www.ruby-forum.com/t/calling-constructor-when-static-new-method-is-defined/190874/5 "2010-06-29T21:44:18Z")

</div>

I said “#new” in some places where I shouldn’t have … so corrected  
below in case there was some confusion …
