Project

General

Profile

Actions

Feature #8027

closed

add the possibility to raise an exception in #included, #extended, #prepended, #inherited and break the calling feature

Added by Hanmac (Hans Mackowiak) about 11 years ago. Updated almost 11 years ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-core:53161]

Description

imo an exception in this hook methods should be possible to prevent the adding in the ancestor chain

module A
def self.included(mod)
raise "dont include me"
end
end

module E
include A
end
##>> RuntimeError: dont include me

E.ancestors #=>[E,A]
what i want:
E.ancestors #=>[E]

maybe other hook methods like method_defined should get an "prevent" option too

maybe for not breaking code we could add "before_included" where an exception could be raised

Updated by semaperepelitsa (Simon Perepelitsa) about 11 years ago

I think you are looking for Module.append_features:

module A
def self.append_features(mod)
raise "dont include me"
super
end
end

module E
begin
include A
rescue
end
end

p E.ancestors #=>[E]

If you don't call super it also won't be included.

Updated by Hanmac (Hans Mackowiak) about 11 years ago

ah good to know, but not the best way. What if i want that a Module is extendable but not includable? Then append_features may not what i want

Updated by zzak (zzak _) almost 11 years ago

  • Status changed from Open to Feedback
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0