# Re: best way to dynamically create new instance methods

**URL:** <https://www.ruby-forum.com/t/re-best-way-to-dynamically-create-new-instance-methods/79063>\
**Category:** Ruby\
**Created:** [December 11, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/re-best-way-to-dynamically-create-new-instance-methods/79063 "2006-12-11T23:20:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [December 11, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/re-best-way-to-dynamically-create-new-instance-methods/79063/1 "2006-12-11T23:20:19Z")

</div>

From: James Edward G. II [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> Sent: Monday, December 11, 2006 3:15 PM  
> To: ruby-talk ML  
> Subject: Re: best way to dynamically create new instance methods

> > > def create\_method(name)  
> > > class \<\< self; self end.send(:define\_method, name) { |\*args|  
> > > args.first }  
> > > end

Ooh, look at Mr. Fancy Pants, using his fancy #send to get around  
private methods.

🙂

Really, I’m writing this email not to praise or make fun, but to point  
out that the above technique may not work when 1.9 rolls around.  
(Assuming I remember correctly that 1.9 will/may change so that #send  
honors the public/protected/private state of the method being invoked.)

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [December 11, 2006, 11:30pm UTC](https://www.ruby-forum.com/t/re-best-way-to-dynamically-create-new-instance-methods/79063/2 "2006-12-11T23:30:19Z")

</div>

On Dec 11, 2006, at 4:19 PM, Gavin K. wrote:

> Ooh, look at Mr. Fancy Pants, using his fancy #send to get around  
> private methods.
> 
> 🙂
> 
> Really, I’m writing this email not to praise or make fun, but to point  
> out that the above technique may not work when 1.9 rolls around.  
> (Assuming I remember correctly that 1.9 will/may change so that #send  
> honors the public/protected/private state of the method being  
> invoked.)

Correct. In 1.9 you need to use funcall() in place of send().

James Edward G. II

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [December 11, 2006, 11:34pm UTC](https://www.ruby-forum.com/t/re-best-way-to-dynamically-create-new-instance-methods/79063/3 "2006-12-11T23:34:22Z")

</div>

Hi –

On Tue, 12 Dec 2006, James Edward G. II wrote:

> > > > > end
> 
> Correct. In 1.9 you need to use funcall() in place of send().

And keep a little card in your pocket to remind you which does  
which… 🙂

David

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [December 12, 2006, 12:56am UTC](https://www.ruby-forum.com/t/re-best-way-to-dynamically-create-new-instance-methods/79063/4 "2006-12-12T00:56:43Z")

</div>

On Tue, Dec 12, 2006 at 07:28:50AM +0900, James Edward G. II wrote:

> > invoked.)
> 
> Correct. In 1.9 you need to use funcall() in place of send().

I write such things this way nowadays for that reason:

class \<\< self; self end.class\_eval{ define\_method(name){ |\*args|  
args.first } }
