# Define\_method vs eval "def ..."

**URL:** <https://www.ruby-forum.com/t/define-method-vs-eval-def/101209>\
**Category:** Ruby\
**Created:** [August 3, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/define-method-vs-eval-def/101209 "2006-08-03T15:46:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmwr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de053/32.png) [@fmwr](https://www.ruby-forum.com/u/fmwr)\
**Post date:** [August 3, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/define-method-vs-eval-def/101209/1 "2006-08-03T15:46:00Z")

</div>

Hello,

Just wondering, with eval “def …” I can do:  
eval “def #{tag}(\*args, &blk) xml(:#{tag}, \*args, &blk) end”

Of course it would be nicer to do:  
define\_method(tag) {|\*args,&blk|, xml(tag, \*args, &blk) }  
But that throws a parse error.

Is there some way to define method that takes a block argument  
without calling eval ?

---

<div class="post-metadata">

**Author:** ![fmwr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de053/32.png) [@fmwr](https://www.ruby-forum.com/u/fmwr)\
**Post date:** [August 3, 2006, 3:55pm UTC](https://www.ruby-forum.com/t/define-method-vs-eval-def/101209/2 "2006-08-03T15:55:04Z")

</div>

On Aug 3, 2006, at 9:45 AM, Tomasz W. wrote:

> without calling eval ?

Ruby 1.9 lets blocks take blocks like define\_method(:meth) { |\*args,  
&blk| … }

Currently theres no way to do it in 1.8 AFAIK.

---

<div class="post-metadata">

**Author:** ![fmwr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de053/32.png) [@fmwr](https://www.ruby-forum.com/u/fmwr)\
**Post date:** [August 3, 2006, 3:55pm UTC](https://www.ruby-forum.com/t/define-method-vs-eval-def/101209/3 "2006-08-03T15:55:04Z")

</div>

Hi,

At Thu, 3 Aug 2006 22:45:40 +0900,  
Tomasz W. wrote in [ruby-talk:205997]:

> Is there some way to define method that takes a block argument  
> without calling eval ?

Using 1.9.

---

<div class="post-metadata">

**Author:** ![fmwr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de053/32.png) [@fmwr](https://www.ruby-forum.com/u/fmwr)\
**Post date:** [August 4, 2006, 12:44am UTC](https://www.ruby-forum.com/t/define-method-vs-eval-def/101209/4 "2006-08-04T00:44:13Z")

</div>

On 8/3/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Nobu Nakada

and it seems to make it into Ruby2 since June  
[http://pub.cozmixng.org/~the-rwiki/?cmd=view;name=Ruby2.0BlockParameterAndMultipleValueAssignment](http://pub.cozmixng.org/~the-rwiki/?cmd=view;name=Ruby2.0BlockParameterAndMultipleValueAssignment)  
which I consider extremly good news 🙂

Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![fmwr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de053/32.png) [@fmwr](https://www.ruby-forum.com/u/fmwr)\
**Post date:** [August 4, 2006, 3:36am UTC](https://www.ruby-forum.com/t/define-method-vs-eval-def/101209/5 "2006-08-04T03:36:08Z")

</div>

On 8/3/06, Tammo F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How do these play together with the new define\_method ?  
> seem to be ignored. What would happen for this code fragment:  
> That first example is a change from 1.8

Proc.new {|x| x}.call(0,1)  
(irb):58: warning: multiple values for a block parameter (2 for 1)  
from (irb):58  
=\> [0, 1]

I’m not sure that I like this change.

–  
Rick DeNatale

IPMS/USA Region 12 Coordinator  
[http://ipmsr12.denhaven2.com/](http://ipmsr12.denhaven2.com/)

Visit the Project Mercury Wiki Site  
[http://www.mercuryspacecraft.com/](http://www.mercuryspacecraft.com/)

---

<div class="post-metadata">

**Author:** ![fmwr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de053/32.png) [@fmwr](https://www.ruby-forum.com/u/fmwr)\
**Post date:** [August 4, 2006, 1:24am UTC](https://www.ruby-forum.com/t/define-method-vs-eval-def/101209/6 "2006-08-04T01:24:20Z")

</div>

Robert D. wrote:  
[…]

> and it seems to make it into Ruby2 since June  
> [http://pub.cozmixng.org/~the-rwiki/](http://pub.cozmixng.org/~the-rwiki/)?  
> cmd=view;name=Ruby2.0BlockParameterAndMultipleValueAssignment  
> which I consider extremly good news 🙂

I am suprised that the blocks will work in such a way.  
How do these play together with the new define\_method ?

Examples from  
[http://pub.cozmixng.org/~the-rwiki/](http://pub.cozmixng.org/~the-rwiki/)?  
cmd=view;name=Ruby2.0BlockParameterAndMultipleValueAssignment  
:

- Proc.new{|x| x}.call(0,1) returns 0.
- Proc.new{|x,y| [x,y]}.call(0) returns [0,nil].

So missing arguments seem to be set to nil, and additional arguments  
seem to be ignored. What would happen for this code fragment:

proc\_one = Proc.new{|x| x}  
proc\_two = Proc.new{|x,y| [x,y]}

define\_method(:meth\_one) &proc\_one  
define\_method(:meth\_two) &proc\_two

p proc\_one.call(0,1) # returns 0 according to the web page  
p proc\_two.call(1) # returns 1,nil according to the web page

p meth\_one(0,1) # ???  
p meth\_two(1) # ???

Would the last two work and return the same result as the direct proc  
calls?  
If so, how would we define a method via define\_method that takes a  
fixed number of parameters?  
If not, where does the magic happen that while the proc accepts the  
arguments,  
the method defined by the proc does not?

Thanks for your help,

Tammo
