Why do #dup and #clone behave differently with respect to instance methods

F

Florian Gross

Robert said:
"While clone is used to duplicate an object, including its internal state,
dup typically uses the class of the descendent object to create the new
instance."
http://www.ruby-doc.org/docs/rdoc/1.9/classes/Object.html#M000889
I'm interested in the reasoning behind this.

I think the rationale is singleton methods being part of internal state.
I'm actually depending on this behavior for stripping singleton methods
from potentially dangerous objects so if this is to be changed I would
need an alternative way to clone objects without their singleton methods.
Kind regards
robert

More regards,
Florian Gross
 
R

Robert Klemme

Florian Gross said:
I think the rationale is singleton methods being part of internal state.
I'm actually depending on this behavior for stripping singleton methods
from potentially dangerous objects so if this is to be changed I would
need an alternative way to clone objects without their singleton
methods.

Sounds reasonable. Still *something* strikes me as odd: #class still
returns the original class. Why doesn't it return the singleton class?
=> 135026484
^^^^^^^^^
One *could* expect something different here. Any comments?

Regards

robert
 
T

ts

R> Sounds reasonable. Still *something* strikes me as odd: #class still
R> returns the original class. Why doesn't it return the singleton class?

#class always return the class of the object.

R> One *could* expect something different here. Any comments?

class A
end

a = A.new
b = A.new

class << a
def a
end
end

p a.class == b.class


Now do you really expect 'false' ?


Guy Decoux
 
R

Robert Klemme

ts said:
R> Sounds reasonable. Still *something* strikes me as odd: #class still
R> returns the original class. Why doesn't it return the singleton class?

#class always return the class of the object.

R> One *could* expect something different here. Any comments?

class A
end

a = A.new
b = A.new

class << a
def a
end
end

p a.class == b.class


Now do you really expect 'false' ?

Well, I don't since I know it's done otherwise in Ruby. But, one *could*
reasonably. Still (A === a) == true and (A === b) == true. I just wanted
to point out that although there is this anonymous class of the instance
it's not accessible in any way I know of (other than iterating through
ObjectSpace maybe).

An alternative would be that "class << a" could return this singleton
class...

Regards

robert
 
T

ts

R> An alternative would be that "class << a" could return this singleton
R> class...

class return the result of the last expression. There is a RCR to have
Kernel#singleton_class


Guy Decoux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,222
Latest member
patricajohnson51

Latest Threads

Top