# Define\_method and blocks

**URL:** <https://rubytalk.org/t/define-method-and-blocks/10893>\
**Category:** ruby-talk\
**Created:** [5 April 2004 20:06 UTC](https://rubytalk.org/t/define-method-and-blocks/10893 "2004-04-05T20:06:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Florian\_G\_Pflug](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@Florian\_G\_Pflug](https://rubytalk.org/u/Florian_G_Pflug)\
**Post date:** [5 April 2004 20:06 UTC](https://rubytalk.org/t/define-method-and-blocks/10893/1 "2004-04-05T20:06:29Z")

</div>

Hi

I’m trying to add a method that receives a block as an argument via  
define\_method - but none of the following versions work.

Version1: (gives a syntax error - &block doesn’t seem to be allowed in block arguments)  
define\_method(:meth1) do |\*args, &block|  
block.call  
end

Version2: (gives "No block given, altough I called methd2 with a block)  
define\_method(:meth2) do |\*args|  
Proc::new.call  
end

Version3: (“No block given” - the same as before)  
define\_method(:meth3) do |\*args|  
yield  
end

Is it possible to define methods that accept blocks as parameters using  
define\_method()?

greetings, Florian Pflug

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [5 April 2004 20:37 UTC](https://rubytalk.org/t/define-method-and-blocks/10893/2 "2004-04-05T20:37:59Z")

</div>

> Hi
> 
> I’m trying to add a method that receives a block as an argument via  
> define\_method - but none of the following versions work.

[snip]

> Is it possible to define methods that accept blocks as parameters using  
> define\_method()?

I believe that both procs and blocks use the block from the scope in  
which they are defined, making it impossible using define\_method.

> **···**
>
> Florian G. Pflug ([fgp@phlo.org](mailto:fgp@phlo.org)) wrote:
> 
> –  
> Eric Hodel - [drbrain@segment7.net](mailto:drbrain@segment7.net) - [http://segment7.net](http://segment7.net)  
> All messages signed with fingerprint:  
> FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

---

<div class="post-metadata">

**Author:** ![Dan\_Doel](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Dan\_Doel](https://rubytalk.org/u/Dan_Doel)\
**Post date:** [6 April 2004 03:23 UTC](https://rubytalk.org/t/define-method-and-blocks/10893/3 "2004-04-06T03:23:40Z")

</div>

I thought Matz was contemplating &block parameters for blocks for the future,  
but I can’t find the post by searching ruby-talk.

Currently what you want is not possible, but it may be in the future.

- Dan

---

<div class="post-metadata">

**Author:** ![Isambart](https://avatars.discourse-cdn.com/v4/letter/i/94ad74/32.png) [@Isambart](https://rubytalk.org/u/Isambart)\
**Post date:** [6 April 2004 11:34 UTC](https://rubytalk.org/t/define-method-and-blocks/10893/4 "2004-04-06T11:34:29Z")

</div>

> I’m trying to add a method that receives a block as an argument via  
> define\_method - but none of the following versions work.

As it seems you can’t do it with define\_methods, you could try using  
eval. It is slower, but it should work. Try something like this :  
eval %{  
class MyClass  
def meth1(\*args, &block)  
block.call  
end  
end  
}

eval is the most powerful statement in the script languages…

PS : I have not tested it, because I have no ruby interpreter to test  
this on. But the idea should work :o)

Vincent Isambart

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [6 April 2004 12:59 UTC](https://rubytalk.org/t/define-method-and-blocks/10893/5 "2004-04-06T12:59:23Z")

</div>

Florian G. Pflug schrieb:

> Hi

Moin!

> I’m trying to add a method that receives a block as an argument via  
> define\_method - but none of the following versions work.

You can try something like this:

obj = Object.new

sklass = class \<\< obj  
def meth1(\*args, &block)  
\_meth1(args, block)  
end

self  
end

some\_variable = “bar”

sklass.send(:define\_method, :\_meth1) do |args, block|  
block.call(some\_variable, \*args)  
end

obj.meth1(“foo”) { |var, str| p [str, var] } # outputs [“foo”, “bar”]

As you see this way you still get the closure behavior of define\_method.  
(You can access variables from the scope where you defined the method  
from within the method.)

It’s all a bit complex, but this is the only way to do this right now.

Maybe it would be better to define a convenience method for this:

class Class  
def define\_block\_method(name, &block)  
internal\_name = :“\_#{name}”

```
 module_eval %{
   def #{name}(*args, &block)
     #{internal_name}(args, block)
   end
 }

 define_method(internal_name, &block)
 return block

```

end  
end

It’s used like this:

Fixnum.define\_block\_method(:new\_times) do |args, block|  
(1 … self).each { |i| block.call(i) }  
self  
end

5.new\_times { puts “foo” } # outputs 3 "foo"s

> greetings, Florian Pflug

Regards,  
Florian Gross

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [6 April 2004 08:33 UTC](https://rubytalk.org/t/define-method-and-blocks/10893/6 "2004-04-06T08:33:41Z")

</div>

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/90671](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/90671)

> **···**
>
> On Tue, Apr 06, 2004 at 12:23:40PM +0900, Dan Doel wrote:
> 
> > I thought Matz was contemplating &block parameters for blocks for the future,  
> > but I can’t find the post by searching ruby-talk.
> 
> –  
> Running Debian GNU/Linux Sid (unstable)  
> batsman dot geo at yahoo dot com
> 
> Sex, Drugs & Linux Rules  
> – MaDsen Wikholm, [mwikholm@at8.abo.fi](mailto:mwikholm@at8.abo.fi)
