aClass.new([args]) -> class / object?

Hi!

We’ve currently got a problem with this code snippet:

classes = [Decimal.class, Whitespace.class, NonWhitespace.class]
cl = classes[2]
return cl.new([])

In our opinion, it should return an instance of class “NonWhitespace”,
but instead, the compiler says the signature of new is wrong:

re_game_1.rb:28:in `new’: wrong argument type Array (expected Class)
(TypeError)

The signature the compiler asks for seems to be that of the static
method Class.new - which we didn’t use, or did we?

Thankful for any comments,
Guido

Hi –

Hi!

We’ve currently got a problem with this code snippet:

classes = [Decimal.class, Whitespace.class, NonWhitespace.class]
cl = classes[2]
return cl.new()

In our opinion, it should return an instance of class “NonWhitespace”,
but instead, the compiler says the signature of new is wrong:

re_game_1.rb:28:in `new’: wrong argument type Array (expected Class)
(TypeError)

The class of Decimal, Whitespace, and NonWhitespace is Class. So
your array is really:

classes = [ Class, Class, Class ]

So… your #new statement is really:

return Class.new()

…hence the error.

What you want to do is:

classes = [ Decimal, Whitespace, NonWhitespace ]

and then when you index the array, you’ll get the classes themselves.

David

···

On Wed, 17 Jul 2002, Guido Poschta wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi Alan!

The class of Decimal, Whitespace, and NonWhitespace is Class.
Ah ok, thx! Seems I worked with java 2 long :slight_smile:

CU,
Guido