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.
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
for which the first doesn’t work and the other two do is that methods
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
This method is called this way:
new String, “abc”
Beware, however, that there are classes without a new method, for
FixNum, BigNum, Float, NilClass, TrueClass, FalseClass, Symbol.
I hope this helps