Module.private like method

i want to make a class method similar to Module.private in that it
should affect subsequent instance method defentions.

class Foo

def self.rule name
# code such that all subsequent instance methods
# defined in subclasses are registered as part of rule ‘name’
end

end

class Bar < Foo

rule :my_first_rule

the following instance methods magically

become associated with :my_first_rule

def foo
end

def bar
end

end

is this possible, or do i need to do some metaprogramming where methods
which belong to a rule start with the rule name say.

thanks

On 4/25/06, polypus [email protected] wrote:

is this possible, or do i need to do some metaprogramming where methods
which belong to a rule start with the rule name say.

Check this out:

class RulesBased
def self.rule(rule_name)
@current_rule = rule_name
end

def self.method_added(name)
puts “Adding method #{name} under rule #@current_rule
end
end

class Test < RulesBased
rule :foo
def meth1
end
def meth2
end

rule :bar
def meth3
end

def meth4
end
end
END

Ryan

thanks many, you rock. i actually tried method_added but must have been
doing something wrong.

On 4/25/06, polypus [email protected] wrote:

thanks many, you rock. i actually tried method_added but must have been
doing something wrong.

No problem. Also another option is to code the rules engine as a module:

module RulesBased
def rule(rule_name)
@current_rule = rule_name
end

def method_added(name)
puts “Adding method #{name} under rule #@current_rule
end
end

class Test
extend RulesBased

rule :foo
def meth1
end
def meth2
end

rule :bar
def meth3
end
def meth4
end
end
END

This allows you to still subclass from something else.

Ryan

This allows you to still subclass from something else.

Ryan

thanks again, i hadn’t really considered that for my project, but will
probably end up doing it that way.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs