# Class\_eval and using a block instead of a String?

**URL:** <https://www.ruby-forum.com/t/class-eval-and-using-a-block-instead-of-a-string/80034>\
**Category:** Ruby\
**Created:** [December 20, 2006, 10:40pm UTC](https://www.ruby-forum.com/t/class-eval-and-using-a-block-instead-of-a-string/80034 "2006-12-20T22:40:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mariano\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eada6e/32.png) [@Mariano\_K](https://www.ruby-forum.com/u/Mariano_K)\
**Post date:** [December 20, 2006, 10:40pm UTC](https://www.ruby-forum.com/t/class-eval-and-using-a-block-instead-of-a-string/80034/1 "2006-12-20T22:40:23Z")

</div>

Hi,

given the following contrived example I am wondering how to use a  
block with class\_eval instead of using the string like in the code  
below?!

Any ideas?

Cheers,  
Mariano

require ‘test/unit’  
include Test::Unit::Assertions

class Base  
class \<\< self  
def enhance(\*attributes)  
class\_eval do  
attr\_reader \*attributes  
end

```
   initializer = "def initialize(#{attributes.join(", ")})"
   attributes.each {|a| initializer << "@#{a}=#{a}\n"}
   initializer << "end"

  # ------
  class_eval initializer # <-------
  # ------
 end

```

end  
end  
class Derived \< Base; end

Derived.enhance(:a, :b, :c)  
d = Derived.new(“a”, “b”, “c”)

assert\_equal “a”, d.a  
assert\_equal “b”, d.b  
assert\_equal “c”, d.c
