[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Constructors

exiquio

11/3/2008 3:59:00 PM

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.
4 Answers

Stefano Crocco

11/3/2008 4:11:00 PM

0

Alle Monday 03 November 2008, exiquio ha scritto:
> 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.

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

Robert Klemme

11/3/2008 5:48:00 PM

0

On 03.11.2008 17:11, Stefano Crocco 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

exiquio

11/3/2008 11:26:00 PM

0

Robert Klemme wrote:
> On 03.11.2008 17:11, Stefano Crocco 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

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.

Ron Fox

11/4/2008 12:53:00 PM

0

exiquio wrote:
> Robert Klemme wrote:
>> On 03.11.2008 17:11, Stefano Crocco 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
>
> 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
> trying to learn. Hence the new operator. Just for the experience.



--
Ron Fox
NSCL
Michigan State University
East Lansing, MI 48824-1321