# Metaprogramming and class\_eval

**URL:** <https://www.ruby-forum.com/t/metaprogramming-and-class-eval/88486>\
**Category:** Ruby\
**Created:** [March 11, 2007, 11:25pm UTC](https://www.ruby-forum.com/t/metaprogramming-and-class-eval/88486 "2007-03-11T23:25:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/919ad9/32.png) [@pedro](https://www.ruby-forum.com/u/pedro)\
**Post date:** [March 11, 2007, 11:25pm UTC](https://www.ruby-forum.com/t/metaprogramming-and-class-eval/88486/1 "2007-03-11T23:25:05Z")

</div>

Hi

Whats the differents between this three classes. Why the  
my\_attr\_accesor dosent create getters and setter clases.

Thanks in advance

#----------------------------------------------------------------------

# 

#----------------------------------------------------------------------

class Class  
def my\_attr\_accessor( \*args )  
args.each do |name|  
self.class.class\_eval do  
attr\_accessor :"#{name}"  
end  
end  
end  
end  
=\> nil

class MyNewClass  
my\_attr\_accessor :id, :diagram, :telegram  
end  
=\> [:id, :diagram, :telegram]

class MyClass  
attr\_accessor :id, :diagram, :telegram  
end  
=\> nil

class Another\_class  
def initialize  
[“id”,“diagram”,“telegram”].each do |name|  
self.class.class\_eval do  
attr\_accessor :"#{name}"  
end  
end  
end  
end  
=\> nil

mnc = MyNewClass.new  
=\> #MyNewClass:0xb7b97d0c  
mc = MyClass.new  
=\> #MyClass:0xb7b950fc  
ac = Another\_class.new  
=\> #\<Another\_class:0xb7b82358\>

ac.diagram  
=\> nil  
mc.diagram

## –

Pedro Del G.

Email : removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![pedro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/919ad9/32.png) [@pedro](https://www.ruby-forum.com/u/pedro)\
**Post date:** [March 12, 2007, 12:42am UTC](https://www.ruby-forum.com/t/metaprogramming-and-class-eval/88486/2 "2007-03-12T00:42:12Z")

</div>

Hi,

You seem to have an extra .class in your modification to Class. You can  
get  
away with this:

class Class  
def my\_attr\_accessor( \*args )  
self.class\_eval do  
attr\_accessor \*args  
end  
end  
end

Explanation: When you’re invoking my\_attr\_accessor from within the class  
def  
of MyNewClass, ‘self’ is the class object MyNewClass (an instance of  
Class).  
You need to call class\_eval on this object. When the block passed into  
class\_eval is executed, ‘self’ is again the class object MyNewClass.  
This is  
what allows you to access the private ‘attr\_accessor’ method.

Mushfeq.

---

<div class="post-metadata">

**Author:** ![pedro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/919ad9/32.png) [@pedro](https://www.ruby-forum.com/u/pedro)\
**Post date:** [March 12, 2007, 4:28am UTC](https://www.ruby-forum.com/t/metaprogramming-and-class-eval/88486/3 "2007-03-12T04:28:30Z")

</div>

On Mar 11, 7:41 pm, “Mushfeq K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> end

You are already where you need to be…

class Class  
def my\_attr\_accessor( \*args )  
attr\_accessor \*args  
end  
end

T.
