# Defining methods dynamically

**URL:** <https://rubytalk.org/t/defining-methods-dynamically/41787>\
**Category:** ruby-talk\
**Created:** [30 October 2007 13:11 UTC](https://rubytalk.org/t/defining-methods-dynamically/41787 "2007-10-30T13:11:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Venkat\_Bagam](https://avatars.discourse-cdn.com/v4/letter/v/df788c/32.png) [@Venkat\_Bagam](https://rubytalk.org/u/Venkat_Bagam)\
**Post date:** [30 October 2007 13:11 UTC](https://rubytalk.org/t/defining-methods-dynamically/41787/1 "2007-10-30T13:11:05Z")

</div>

Hi Folks,

I have an array has\_roles = %w[admin employee ...] which is a subset of  
application\_roles = ["admin", "employee", "manager"...]  
Now I would define a method for each role in application roles where i  
would call methods like

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@logged\_in\_user.admin which returns true because has\_roles  
includes "admin"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@logged\_in\_user.employee which returns true because has\_roles  
includes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"employee"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@logged\_in\_user.manager which returns false because has\_roles  
doesnot  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include "manager"

I came accross using define\_method in class Module for this purpose but  
went wrong some where. Can any one there achieve this using  
define\_method. Its very urgent any help appreciated..thanks in  
advance...

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jon\_Hawkins](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jon\_Hawkins](https://rubytalk.org/u/Jon_Hawkins)\
**Post date:** [30 October 2007 15:22 UTC](https://rubytalk.org/t/defining-methods-dynamically/41787/2 "2007-10-30T15:22:34Z")

</div>

Venkat Bagam wrote:

> Hi Folks,
> 
> I have an array has\_roles = %w[admin employee ...] which is a subset of  
> application\_roles = ["admin", "employee", "manager"...]  
> Now I would define a method for each role in application roles where i  
> would call methods like
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@logged\_in\_user.admin which returns true because has\_roles  
> includes "admin"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@logged\_in\_user.employee which returns true because has\_roles  
> includes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"employee"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@logged\_in\_user.manager which returns false because has\_roles  
> doesnot  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include "manager"
> 
> I came accross using define\_method in class Module for this purpose but  
> went wrong some where. Can any one there achieve this using  
> define\_method. Its very urgent any help appreciated..thanks in  
> advance...

If I'm understanding you right...

class Roles  
class \<\< self

def admin  
puts "This person is an admin"  
end

def employee  
puts "This person is an employee"  
end

end

Of course you probably wouldnt use this very example to achieve what  
your going for, but this is one of the approaches you would take using  
the define method.

- Mac

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [30 October 2007 15:46 UTC](https://rubytalk.org/t/defining-methods-dynamically/41787/3 "2007-10-30T15:46:38Z")

</div>

Sure that is the hammer for your nail:  
Not tested  
ar.each do | role |  
&nbsp;&nbsp;&nbsp;define\_method role do |\*args|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@logged\_in\_usr.send( role )  
&nbsp;&nbsp;&nbsp;end  
end  
Cheers  
Robert

> **···**
>
> On 10/30/07, Venkat Bagam \<bagam\_venkat@hotmail.com\> wrote:
> 
> > Hi Folks,
> > 
> > I have an array has\_roles = %w[admin employee ...] which is a subset of  
> > application\_roles = ["admin", "employee", "manager"...]  
> > Now I would define a method for each role in application roles where i  
> > would call methods like
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@logged\_in\_user.admin which returns true because has\_roles  
> > includes "admin"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@logged\_in\_user.employee which returns true because has\_roles  
> > includes  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"employee"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@logged\_in\_user.manager which returns false because has\_roles  
> > doesnot  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include "manager"
> 
> > I came accross using define\_method in class Module for this purpose but  
> > went wrong some where. Can any one there achieve this using  
> > define\_method. Its very urgent any help appreciated..thanks in  
> > advance...
> 
> --  
> what do I think about Ruby?  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

---

<div class="post-metadata">

**Author:** ![Drew\_Olson](https://avatars.discourse-cdn.com/v4/letter/d/c67d28/32.png) [@Drew\_Olson](https://rubytalk.org/u/Drew_Olson)\
**Post date:** [30 October 2007 18:13 UTC](https://rubytalk.org/t/defining-methods-dynamically/41787/4 "2007-10-30T18:13:52Z")

</div>

Venkat Bagam wrote:

> Hi Folks,
> 
> I have an array has\_roles = %w[admin employee ...] which is a subset of  
> application\_roles = ["admin", "employee", "manager"...]

I personally like this approach. It's all a matter of taste:

class Application  
&nbsp;&nbsp;@@possible\_roles = [:admin, :employee, :manager]

&nbsp;&nbsp;def self.roles \*args  
&nbsp;&nbsp;&nbsp;&nbsp;args.each do |role|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(role) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;(@@possible\_roles - args).each do |non\_role|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(non\_role) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

class MyApplication \< Application  
&nbsp;&nbsp;roles :admin, :employee  
end

my\_app = MyApplication.new  
puts my\_app.admin # =\> true  
puts my\_app.manager # =\> false

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [30 October 2007 15:49 UTC](https://rubytalk.org/t/defining-methods-dynamically/41787/5 "2007-10-30T15:49:00Z")

</div>

sorry hit this send button agaiiin

however this seems simple enough for some sort of delegation

HTH  
Robert

> **···**
>
> --  
> what do I think about Ruby?  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

---

<div class="post-metadata">

**Author:** ![Venkat\_Bagam](https://avatars.discourse-cdn.com/v4/letter/v/df788c/32.png) [@Venkat\_Bagam](https://rubytalk.org/u/Venkat_Bagam)\
**Post date:** [3 November 2007 10:09 UTC](https://rubytalk.org/t/defining-methods-dynamically/41787/6 "2007-11-03T10:09:58Z")

</div>

Drew Olson wrote:

> Venkat Bagam wrote:
> 
> > Hi Folks,
> > 
> > I have an array has\_roles = %w[admin employee ...] which is a subset of  
> > application\_roles = ["admin", "employee", "manager"...]
> 
> I personally like this approach. It's all a matter of taste:
> 
> class Application  
> &nbsp;&nbsp;@@possible\_roles = [:admin, :employee, :manager]
> 
> &nbsp;&nbsp;def self.roles \*args  
> &nbsp;&nbsp;&nbsp;&nbsp;args.each do |role|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class\_eval do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(role) do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;(@@possible\_roles - args).each do |non\_role|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class\_eval do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(non\_role) do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;false  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> class MyApplication \< Application  
> &nbsp;&nbsp;roles :admin, :employee  
> end
> 
> my\_app = MyApplication.new  
> puts my\_app.admin # =\> true  
> puts my\_app.manager # =\> false

Hi thanks for the reply... Its working well but I just need some  
illustration.

1. whats happening when the my\_app object is created.?  
2. whats happening here def self.roles \*args  
&nbsp;&nbsp;&nbsp;&nbsp;I mean, how come \*args take :admin, :employee into it?

thanks and regards  
Venkat

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Dan\_Yoder](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dan\_Yoder](https://rubytalk.org/u/Dan_Yoder)\
**Post date:** [30 October 2007 17:45 UTC](https://rubytalk.org/t/defining-methods-dynamically/41787/7 "2007-10-30T17:45:07Z")

</div>

You can definitely do this with define\_method:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 class Person  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 attr\_accessor :roles  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4 end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5 Person.roles = %w( admin manager employee )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7 attr\_accessor :roles  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8 def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;9 @roles =   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10 end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12 Person.roles.each do |role|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;13 define\_method( role ) { || roles.include? role }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;14 end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;16 end

john = Person.new  
john.admin # false  
john.roles \<\< 'admin'  
john.admin # true

Robert mentions delegation as an option, which would also work:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 require 'forwardable'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 class Person  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 extend Forwardable  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4 class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5 attr\_accessor :roles  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6 end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7 Person.roles = %w( admin manager employee )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;9 attr\_accessor :roles  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10 def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11 @roles =   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12 end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;13  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;14 Person.roles.each do |role|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15 def\_delegator :@roles, role, :include?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;16 end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;17  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;18 end

One difficulty with either implementation is that you need to make  
sure that you add (or remove) the appropriate method whenever  
Person.roles is changed. If Person.roles isn't going to change, then  
just change the singleton's attr\_accessor for roles to attr\_reader.  
Otherwise, you are going to need singleton methods like 'add\_role' and  
'remove\_role', which will define or undefine your role methods.

Hope that helps.

Dan

> **···**
>
> ---  
> Dan Yoder  
> [http://dev.zeraweb.com/](http://dev.zeraweb.com/)  
> Ruby and JavaScript Consulting
> 
> On Oct 30, 8:49 am, "Robert Dober" \<robert.do...@gmail.com\> wrote:
> 
> > sorry hit this send button agaiiin
> > 
> > however this seems simple enough for some sort of delegation
> > 
> > HTH  
> > Robert
> > 
> > --  
> > what do I think about Ruby?[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [3 November 2007 16:56 UTC](https://rubytalk.org/t/defining-methods-dynamically/41787/8 "2007-11-03T16:56:50Z")

</div>

> You can definitely do this with define\_method:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 class Person  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 class \<\< self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 attr\_accessor :roles  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4 end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5 Person.roles = %w( admin manager employee )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7 attr\_accessor :roles  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8 def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;9 @roles =   
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10 end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12 Person.roles.each do |role|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;13 define\_method( role ) { || roles.include? role }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;14 end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;16 end
> 
> john = Person.new  
> john.admin # false  
> john.roles \<\< 'admin'  
> john.admin # true
> 
> Robert mentions delegation as an option, which would also work:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 require 'forwardable'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 class Person  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 extend Forwardable  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4 class \<\< self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5 attr\_accessor :roles  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6 end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7 Person.roles = %w( admin manager employee )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;9 attr\_accessor :roles  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10 def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11 @roles =   
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12 end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;13  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;14 Person.roles.each do |role|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15 def\_delegator :@roles, role, :include?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;16 end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;17  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;18 end
> 
> One difficulty with either implementation is that you need to make  
> sure that you add (or remove) the appropriate method whenever  
> Person.roles is changed.

Exactly that was why I thaught of delegation, as I am not familiar  
with the delegation idioms please forgive me that I handcoded it

class Person  
&nbsp;&nbsp;@roles = %w{ a b c d }  
&nbsp;&nbsp;class \<\< self; attr\_reader :roles end  
&nbsp;&nbsp;def method\_missing name,\*args,&blk  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super unless @roles.map.to\_s.include? name  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;### do delegation here  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@some\_obj.send name, \*args, &blk  
&nbsp;&nbsp;end

> Hope that helps.

Idem 😉

> Dan
> 
> ---  
> Dan Yoder  
> [http://dev.zeraweb.com/](http://dev.zeraweb.com/)  
> Ruby and JavaScript Consulting

Robert

> **···**
>
> On Oct 30, 2007 6:45 PM, Dan Yoder \<dan@zeraweb.com\> wrote:
> 
> --  
> what do I think about Ruby?  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [3 November 2007 16:58 UTC](https://rubytalk.org/t/defining-methods-dynamically/41787/9 "2007-11-03T16:58:04Z")

</div>

Erratuml

&nbsp;&nbsp;&nbsp;unless @roles.map.to\_s

--\>  
&nbsp;&nbsp;&nbsp;unless self.class.roles.map.to\_s

Cheers  
Robert

> **···**
>
> --  
> what do I think about Ruby?  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

---

<div class="post-metadata">

**Author:** ![RaL](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@RaL](https://rubytalk.org/u/RaL)\
**Post date:** [3 November 2007 18:10 UTC](https://rubytalk.org/t/defining-methods-dynamically/41787/10 "2007-11-03T18:10:01Z")

</div>

> Exactly that was why I thaught of delegation, as I am not familiar  
> with the delegation idioms please forgive me that I handcoded it
> 
> class Person  
> &nbsp;&nbsp;@roles = %w{ a b c d }  
> &nbsp;&nbsp;class \<\< self; attr\_reader :roles end  
> &nbsp;&nbsp;def method\_missing name,\*args,&blk  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super unless @roles.map.to\_s.include? name  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;### do delegation here  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@some\_obj.send name, \*args, &blk  
> &nbsp;&nbsp;end

method\_missing - is awfully slow, so if there is other option (like two from Dan Yoder) than better to use them at the cost of a few more lines.

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [4 November 2007 01:14 UTC](https://rubytalk.org/t/defining-methods-dynamically/41787/11 "2007-11-04T01:14:52Z")

</div>

I was showing the concept and I asked to imagine that delegation was  
used, did I not?  
However I do not think that the speed of method missing might be an  
issue and dynamic delegation would probably be slow too.  
Now if speed really is an issue you could of course do something like  
the following  
class DynamicDefiners  
&nbsp;&nbsp;def initialize klass  
&nbsp;&nbsp;@klass = klass  
&nbsp;&nbsp;@names = # not used in this simple usecase  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def add \*names  
&nbsp;&nbsp;@names += names  
&nbsp;&nbsp;names.each do | name |  
&nbsp;&nbsp;&nbsp;&nbsp;@klass.module\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor name # or more sophisticated stuff as defining delegators  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def del \*names  
&nbsp;&nbsp;@names -= names  
&nbsp;&nbsp;names.each do | name |  
&nbsp;&nbsp;&nbsp;&nbsp;@klass.module\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;remove\_method name # or more sophisticated stuff as deleting delegators  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;end  
end  
class Person  
&nbsp;&nbsp;@roles = DynamicDefiners.new self  
&nbsp;&nbsp;@roles.add \*%w{ foo bar }  
&nbsp;&nbsp;class \<\< self; attr\_reader :roles end  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@foo = 22  
&nbsp;&nbsp;&nbsp;&nbsp;@bar = 32  
&nbsp;&nbsp;&nbsp;&nbsp;@baz = 42  
&nbsp;&nbsp;end  
end

p = Person.new  
puts( p.foo )  
puts( p.bar ) rescue puts "no bar"  
puts( p.baz ) rescue puts "no baz"

Person.roles.add "baz"  
Person.roles.del "bar"

puts( p.foo )  
puts( p.bar ) rescue puts "no bar"  
puts( p.baz ) rescue puts "no baz"

I do not think one would need this stuff but who knows 😉

R.

> **···**
>
> On Nov 3, 2007 7:10 PM, brainopia \<ravwar@gmail.com\> wrote:
> 
> > \> Exactly that was why I thaught of delegation, as I am not familiar  
> > \> with the delegation idioms please forgive me that I handcoded it  
> > \>  
> > \> class Person  
> > \> @roles = %w{ a b c d }  
> > \> class \<\< self; attr\_reader :roles end  
> > \> def method\_missing name,\*args,&blk  
> > \> super unless @roles.map.to\_s.include? name  
> > \> ### do delegation here  
> > \> @some\_obj.send name, \*args, &blk  
> > \> end
> > 
> > method\_missing - is awfully slow, so if there is other option (like two  
> > from Dan Yoder) than better to use them at the cost of a few more lines.
> 
> --  
> what do I think about Ruby?  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
