# Constructors

**URL:** <https://www.ruby-forum.com/t/constructors/152409>\
**Category:** Ruby\
**Created:** [November 3, 2008, 5:00pm UTC](https://www.ruby-forum.com/t/constructors/152409 "2008-11-03T17:00:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![exiquio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@exiquio](https://www.ruby-forum.com/u/exiquio)\
**Post date:** [November 3, 2008, 5:00pm UTC](https://www.ruby-forum.com/t/constructors/152409/1 "2008-11-03T17:00:53Z")

</div>

The following is the beginning of a Java-esque new operator.

def new o  
o.class == Class ? o.new : o  
end

Used with the String class you can do:

new String # =\> “”  
new String(‘foo’) # =\> “foo”

But this is not the case with a class like Object:

new Object # okay  
new Object() # error

My question is, what are classes like Array and String defining that  
Object isn’t? And how can I define my own? Thanks in advanced.

---

<div class="post-metadata">

**Author:** ![exiquio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@exiquio](https://www.ruby-forum.com/u/exiquio)\
**Post date:** [November 3, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/constructors/152409/2 "2008-11-03T17:16:09Z")

</div>

Alle Monday 03 November 2008, exiquio ha scritto:

> But this is not the case with a class like Object:
> 
> new Object # okay  
> new Object() # error
> 
> My question is, what are classes like Array and String defining that  
> Object isn’t? And how can I define my own? Thanks in advanced.

The code

Object()

is interpreted as a method call to a method called Object passing no  
arguments. The same happens with String(“abc”) or Array(‘x’). The only  
reason  
for which the first doesn’t work and the other two do is that methods  
called  
String and Array exist, while a method called Object doesn’t.

I think the closest you can get to the Java syntax is this:

def new o, \*args  
o.class == Class ? o.new(\*args) : o  
end

This method is called this way:

new String, “abc”  
new Object

Beware, however, that there are classes without a new method, for  
example  
FixNum, BigNum, Float, NilClass, TrueClass, FalseClass, Symbol.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![exiquio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@exiquio](https://www.ruby-forum.com/u/exiquio)\
**Post date:** [November 3, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/constructors/152409/3 "2008-11-03T18:53:20Z")

</div>

On 03.11.2008 17:11, Stefano C. wrote:

> I think the closest you can get to the Java syntax is this:
> 
> def new o, \*args  
> o.class == Class ? o.new(\*args) : o  
> end

Can someone please explain to me what we gain by doing this?

robert

---

<div class="post-metadata">

**Author:** ![exiquio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@exiquio](https://www.ruby-forum.com/u/exiquio)\
**Post date:** [November 4, 2008, 12:30am UTC](https://www.ruby-forum.com/t/constructors/152409/4 "2008-11-04T00:30:45Z")

</div>

Robert K. wrote:

> robert  
> I didn’t mean to imply that Ruby needs this. It doesnt. I think that  
> JavaScript is a very fascinating language and I am slowly trying to  
> implement core JavaScript in Ruby, another enjoyable language I am  
> trying to learn. Hence the new operator. Just for the experience.

---

<div class="post-metadata">

**Author:** ![exiquio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@exiquio](https://www.ruby-forum.com/u/exiquio)\
**Post date:** [November 6, 2008, 9:44am UTC](https://www.ruby-forum.com/t/constructors/152409/5 "2008-11-06T09:44:18Z")

</div>

On Tue, 2008-11-04 at 02:48 +0900, Robert K. wrote:

> On 03.11.2008 17:11, Stefano C. wrote:
> 
> > I think the closest you can get to the Java syntax is this:
> > 
> > def new o, \*args  
> > o.class == Class ? o.new(\*args) : o  
> > end
> 
> Can someone please explain to me what we gain by doing this?

I am frequently converting Java code to Ruby using cut & paste. At most  
the code needs about 10% changing to run, and the “new” operator is one  
of those changes. If I didn’t have to do it, the first step of  
converting, getting it to run, will go much faster.

I always convert completely to ruby conventions, but I appreciate being  
able to do it in small steps.

If the new operator, and accessing class constants using Class.CONSTANT  
was available, step 1 would be smaller.

I would only use this for development.

---

<div class="post-metadata">

**Author:** ![exiquio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@exiquio](https://www.ruby-forum.com/u/exiquio)\
**Post date:** [November 4, 2008, 1:56pm UTC](https://www.ruby-forum.com/t/constructors/152409/6 "2008-11-04T13:56:20Z")

</div>

exiquio wrote:

> > robert
> 
> I didn’t mean to imply that Ruby needs this. It doesnt. I think that  
> JavaScript is a very fascinating language and I am slowly trying to  
> implement core JavaScript in Ruby,  
> Why? If purely as a learning project fine… but remember language a  
> is not language b no matter how much you try to coerce it to be.  
> Idioms are different, patterns are different and so on.  
> another enjoyable language I am
