# Module.private like method

**URL:** <https://www.ruby-forum.com/t/module-private-like-method/58796>\
**Category:** Ruby\
**Created:** [April 26, 2006, 12:41am UTC](https://www.ruby-forum.com/t/module-private-like-method/58796 "2006-04-26T00:41:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![polypus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@polypus](https://www.ruby-forum.com/u/polypus)\
**Post date:** [April 26, 2006, 12:41am UTC](https://www.ruby-forum.com/t/module-private-like-method/58796/1 "2006-04-26T00:41:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![polypus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@polypus](https://www.ruby-forum.com/u/polypus)\
**Post date:** [April 26, 2006, 12:55am UTC](https://www.ruby-forum.com/t/module-private-like-method/58796/2 "2006-04-26T00:55:17Z")

</div>

On 4/25/06, polypus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![polypus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@polypus](https://www.ruby-forum.com/u/polypus)\
**Post date:** [April 26, 2006, 1:07am UTC](https://www.ruby-forum.com/t/module-private-like-method/58796/3 "2006-04-26T01:07:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![polypus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@polypus](https://www.ruby-forum.com/u/polypus)\
**Post date:** [April 26, 2006, 1:20am UTC](https://www.ruby-forum.com/t/module-private-like-method/58796/4 "2006-04-26T01:20:09Z")

</div>

On 4/25/06, polypus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![polypus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@polypus](https://www.ruby-forum.com/u/polypus)\
**Post date:** [May 2, 2006, 12:05am UTC](https://www.ruby-forum.com/t/module-private-like-method/58796/5 "2006-05-02T00:05:40Z")

</div>

> 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.
