# How to define methods dynamically using define\_method

**URL:** <https://www.ruby-forum.com/t/how-to-define-methods-dynamically-using-define-method/196506>\
**Category:** Rails\
**Created:** [October 30, 2007, 2:12pm UTC](https://www.ruby-forum.com/t/how-to-define-methods-dynamically-using-define-method/196506 "2007-10-30T14:12:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [October 30, 2007, 2:12pm UTC](https://www.ruby-forum.com/t/how-to-define-methods-dynamically-using-define-method/196506/1 "2007-10-30T14:12:28Z")

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

```
   @logged_in_user.admin which returns true because has_roles

```

includes “admin”  
@logged\_in\_user.employee which returns true because has\_roles  
includes  
“employee”  
@logged\_in\_user.manager which returns false because has\_roles  
doesnot  
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…
