Reopen a class and inheritance

P

pachl

Is it recommended to include the parent class when reopening a child
class? What are the consequences of each?

class Parent
def meth0; 'meth0' end
end

class Child < Parent
def meth1; 'meth1' end
end

# Reopen class w/o parent
class Child
def meth2; 'meth2' end
end

# Reopen class w/ parent
class Child < Parent
def meth3; 'meth3' end
end
 
T

ThoML

Is it recommended to include the parent class when reopening a child

If you change the parent class (for whatever reason that might be),
you'll also have to change all locations where the child class is
opened.

IIRC it was once discussed that adding the child class makes added
methods be stacked instead of overwritten. I hope this won't be
included in future versions of ruby due to its cryptic style but there
is a possibility these two versions will behave differently some day
in the future.
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top