# Blocks in class\_eval

**URL:** <https://www.ruby-forum.com/t/blocks-in-class-eval/86775>\
**Category:** Ruby\
**Created:** [February 25, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/blocks-in-class-eval/86775 "2007-02-25T20:15:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyle\_Mitchell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5f8ce5/32.png) [@Kyle\_Mitchell](https://www.ruby-forum.com/u/Kyle_Mitchell)\
**Post date:** [February 25, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/blocks-in-class-eval/86775/1 "2007-02-25T20:15:40Z")

</div>

the following works:

///// BEGIN funcmeta.rb /////  
def func\_meta(&b)  
class\_eval {  
@@proc = b #guilt begins here  
def test  
@@proc.call  
end  
}  
end

class Test  
func\_meta { puts(“test”) }  
end

t = Test.new  
t.test  
///// END funcmeta.rb /////

any clean way to do this without resorting to a class variable?

thanks in advance, you darned helpful people!

~Kyle

---

<div class="post-metadata">

**Author:** ![Kyle\_Mitchell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5f8ce5/32.png) [@Kyle\_Mitchell](https://www.ruby-forum.com/u/Kyle_Mitchell)\
**Post date:** [February 25, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/blocks-in-class-eval/86775/2 "2007-02-25T20:37:05Z")

</div>

On Mon, 26 Feb 2007, Kyle Mitchell wrote:

> end
> 
> thanks in advance, you darned helpful people!

harp:~ \> cat a.rb  
class Module  
def func\_meta m, &b  
module\_eval{ define\_method m, &b }  
end  
end

class Test  
func\_meta(“test”){ puts “test” }  
end

t = Test.new  
t.test

harp:~ \> ruby a.rb  
test

-a

---

<div class="post-metadata">

**Author:** ![Kyle\_Mitchell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5f8ce5/32.png) [@Kyle\_Mitchell](https://www.ruby-forum.com/u/Kyle_Mitchell)\
**Post date:** [February 25, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/blocks-in-class-eval/86775/3 "2007-02-25T20:39:05Z")

</div>

Kyle Mitchell wrote:

> end
> 
> thanks in advance, you darned helpful people!
> 
> ~Kyle

def func\_meta(&b)  
class\_eval {  
define\_method :test, &b  
}  
end

class Test  
func\_meta { puts(“test”) }  
end

t = Test.new  
t.test
