[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Why should initialize_copy verify the class of the original?

Jeremy Henty

2/18/2007 4:09:00 PM

I was looking at the CD player example in the Pickaxe book and I can't
see why their C implementation of CDPlayer#initialize_copy needs to
check that the original object is an instance of CDPlayer . Isn't
this always the case? If #initialize_copy is only called from inside
#dup or #clone then surely the receiver has to be of the same class as
the original! What am I missing?

Regards,

Jeremy Henty
6 Answers

SonOfLilit

2/18/2007 4:49:00 PM

0

I can call initialize_copy too, here:

object.initialize_copy

Ha! I've fooled this object!

In Ruby, you can always do anything. No, really.

If $SAFE isn't used (which is mostly the case), every bit of code can
do ANYTHING to the program state.

So that initialize_copy sanity test makes sense (personally, I'm not
sure I would've done it, but it makes sense).


Plug: Check the adopt-a-newbie thread in ruby-talk.

On 2/18/07, Jeremy Henty <jeremy@chaos.org.uk> wrote:
> I was looking at the CD player example in the Pickaxe book and I can't
> see why their C implementation of CDPlayer#initialize_copy needs to
> check that the original object is an instance of CDPlayer . Isn't
> this always the case? If #initialize_copy is only called from inside
> #dup or #clone then surely the receiver has to be of the same class as
> the original! What am I missing?
>
> Regards,
>
> Jeremy Henty
>
>

Jeremy Henty

2/18/2007 6:08:00 PM

0

On 2007-02-18, SonOfLilit <sonoflilit@gmail.com> wrote:
> I can call initialize_copy too, here:
>
> object.initialize_copy

Well, actually you *can't*:

==> NoMethodError: private method `initialize_copy' called for ...

:-) but I take your point (and you can work round "private" with
#instance_eval ). But can it ever be called with an object of the
wrong type unless the user explicitly does so?

> If $SAFE isn't used (which is mostly the case), every bit of code can
> do ANYTHING to the program state.

That's true for Ruby variables, method calls etc., but hopefully we
can ensure the C internals are not corrupted. I was thinking I could
at least rely on the method being called with objects of the right
type, but as you pointed out that's not true.

> So that initialize_copy sanity test makes sense (personally, I'm not
> sure I would've done it,

I didn't do it either when I implemented color classes for Ruby/FLTK.
I haven't managed to get a segfault out of it, but I can certainly
corrupt objects. One more for the bug list...

Thanks for the answer, I'm a little embarrassed not to have thought of
it before posting.

Jeremy Henty

David Vallner

2/19/2007 2:16:00 PM

0

On Sun, 18 Feb 2007 17:10:06 +0100, Jeremy Henty <jeremy@chaos.org.uk>
wrote:

> I was looking at the CD player example in the Pickaxe book and I can't
> see why their C implementation of CDPlayer#initialize_copy needs to
> check that the original object is an instance of CDPlayer . Isn't
> this always the case? If #initialize_copy is only called from inside
> #dup or #clone then surely the receiver has to be of the same class as
> the original! What am I missing?
>

CDPlayer.new.instance_eval {initialize_copy(TeddyBear.new)}

Which would probably cause the C implementation to do Bad Things without
said check.

David Vallner

Jeremy Henty

2/20/2007 9:30:00 AM

0

On 2007-02-19, David Vallner <david@vallner.net> wrote:
> On Sun, 18 Feb 2007 17:10:06 +0100, Jeremy Henty <jeremy@chaos.org.uk>
> wrote:
>
>> [...] If #initialize_copy is only called from inside #dup or #clone
>> then surely the receiver has to be of the same class as the
>> original! What am I missing?
>
> CDPlayer.new.instance_eval {initialize_copy(TeddyBear.new)}

That's the conclusion we came to in the other thread (though it wasn't
written down explicitly as you did here).

> Which would probably cause the C implementation to do Bad Things
> without said check.

Yes, if the instances of the CDPlayer and TeddyBear classes are T_DATA
objects that wrap (CDPlayer *) and (TeddyBear *) pointers, then this
code will coerce a (TeddyBear *) to a (CDPlayer *). Hilarity may well
ensue.

Incidentally the Pickaxe is (IMHO) a little misleading on this point,
it describes Data_Get_Struct as "a type-safe wrapper around the macro
DATA_PTR(obj)". In fact it's only type-safe with respect to the TYPE
of obj , it's not type-safe with respect to the type of the C pointer
that obj wraps.

Regards,

Jeremy Henty

David Agdern

2/2/2014 8:59:00 PM

0

On Sunday, February 2, 2014 3:44:30 PM UTC-5, Mr. Mike wrote:
> xOn Sat, 1 Feb 2014 12:24:11 -0800 (PST), Willem Orange
>
> <ivanmaxim1@gmail.com> wrote:
>
>
>
> > For me his greatest achievement was his film The Pedestrian (Der Fussganger) about an elderly war criminal. The film also was a homage to the great German films of the past in that the cast was filled with some of the great names of German film going back to the era between the world wars.
>
>
>
> What a pity, this doesn't seem to be available on DVD except in
>
> Germany as part of a very expensive box set.

I recall him sitting aside Leonard Bernstein who is discussing Beethoven's Pastorale Symphony at the piano in some documentary.

Johannes Roehl

2/7/2014 10:37:00 AM

0

Am 02.02.2014 21:59, schrieb JohnGavin:
> On Sunday, February 2, 2014 3:44:30 PM UTC-5, Mr. Mike wrote:
>> xOn Sat, 1 Feb 2014 12:24:11 -0800 (PST), Willem Orange
>>
>> <ivanmaxim1@gmail.com> wrote:
>>
>>> For me his greatest achievement was his film The Pedestrian (Der
>>> Fussganger) about an elderly war criminal. The film also was a
>>> homage to the great German films of the past in that the cast was
>>> filled with some of the great names of German film going back to
>>> the era between the world wars.
>>
>> What a pity, this doesn't seem to be available on DVD except in
>>
>> Germany as part of a very expensive box set.
>
> I recall him sitting aside Leonard Bernstein who is discussing
> Beethoven's Pastorale Symphony at the piano in some documentary.

I vaguely remember a series of Bernstein's Vienna Beethoven Symphonies I
watched as a teenager on German TV in the late '80ties.
There were short introductions and sometimes Schell participated either
by impersonating Beethoven, quoting from some letters or in one case
reciting Goethe's poem "Prometheus" and maybe more similar stuff.