-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
super
and method overwrite mechanism design
#594
Comments
Can you elaborate a bit on this one? Without Also, I'm a bit confused about the relationship between |
Is the monkey-patching moderate like Ruby's |
super
and monkey-patching mechanism designsuper
and method overriden mechanism design
super
and method overriden mechanism designsuper
and method overwrite mechanism design
@saveriomiroddi @hachi8833 Change the title to be method overwriting, does that look better?
class GrandClass
def initialize(foo)
@foo = foo
end
end
class ParentClass < GrandClass; end
module Bar
def initialize(foo)
@foo = foo + 10
end
# some other methods
end
class ChildClass < ParentClass
include Bar
def initialize(foo)
super_from(GrandClass, foo) # This will skip the Bar's initialize
end
end I think this gives users a more clear view and control about the method inheritance. |
So, the term exact is method overriding. I don't think specific support for module overriding initialize() should be added. The reason is that modules are used for composition; such override would interfere with inheritance. Interestingly, this behavior could be considered multiple inheritance, which is not a very "appreciated" design (I've personally never seen it). This is a silly example of why modules should not override a class initializer: class Car
def initialize
start_engine
end
end
module TopOpeningDoors
def initialize
open_doors
end
end
class Ferrari < Car
include TopOpeningDoors
def initialize
super_from(TopOpeningDoors) # won't start_engine
start_turbines
end
end |
@saveriomiroddi I used class User
def to_xml
serialize(:email, :name)
end
end
class Member < User
def to_xml
serialize(:email, :name, :member_id)
end
end
class NullMember < Member
def to_xml
super_from(User)
end
end
NullMember.new.to_xml #=> will only have email and name field |
Ah! I see. Adding doesn't hurt 😄 👍 I think modules overriding should be taken into account as well (eg. the case where |
super
keyword. But we cansuper_from(GrandClass)
to let users explicitly specify which ancestor's method they super tosuper
-like mechanism at allThe text was updated successfully, but these errors were encountered: