# Why do plugins use class\_eval?

**URL:** <https://www.ruby-forum.com/t/why-do-plugins-use-class-eval/51335>\
**Category:** Rails\
**Created:** [December 29, 2005, 9:12pm UTC](https://www.ruby-forum.com/t/why-do-plugins-use-class-eval/51335 "2005-12-29T21:12:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blair\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@Blair\_Z](https://www.ruby-forum.com/u/Blair_Z)\
**Post date:** [December 29, 2005, 9:12pm UTC](https://www.ruby-forum.com/t/why-do-plugins-use-class-eval/51335/1 "2005-12-29T21:12:48Z")

</div>

I’m writing my own plugin and all the plugins I’ve seen use class\_eval  
in the  
method that is called in the including module. For example:

[http://wiki.rubyonrails.com/rails/pages/HowToWriteAnActsAsFoxPlugin](http://wiki.rubyonrails.com/rails/pages/HowToWriteAnActsAsFoxPlugin)

module Foo  
module Acts #:nodoc:  
module Fox #:nodoc:

```
   def self.included(base)
     base.extend(ClassMethods)
   end

   # declare the class level helper methods which
   # will load the relevant instance methods
   # defined below when invoked
   module ClassMethods
     def acts_as_fox
       class_eval do
         extend Foo::Acts::Fox::SingletonMethods
       end
     end

```

…

Why is the class\_eval needed here in acts\_as\_fox? Here self is the  
module that  
called acts\_as\_fox, which I’ve tested by adding a ‘puts self.name’ in  
acts\_as\_fox. Isn’t self.class\_eval redundant? The code could look like

```
     def acts_as_fox
       extend Foo::Acts::Fox::SingletonMethods
     end

```

So I don’t see the point of having that there.

I see the point of this code which is used to add the plugin into  
ActiveRecord::Base since self is not ActiveRecord::Base:

ActiveRecord::Base.class\_eval do  
include Foo::Acts::Fox  
end

but even here, couldn’t this be used instead?

ActiveRecord::Base.include Foo::Acts::Fox

Can anybody shed any light on this?

Regards,  
Blair

–  
Blair Z., Ph.D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subversion and Orca training and consulting  
[http://www.orcaware.com/svn/](http://www.orcaware.com/svn/)

---

<div class="post-metadata">

**Author:** ![Blair\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@Blair\_Z](https://www.ruby-forum.com/u/Blair_Z)\
**Post date:** [December 29, 2005, 9:21pm UTC](https://www.ruby-forum.com/t/why-do-plugins-use-class-eval/51335/2 "2005-12-29T21:21:52Z")

</div>

> Can anybody shed any light on this?

#include is private, so you have to do:

ActiveRecord::Base.send :include, Foo::Acts::Fox

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![Blair\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@Blair\_Z](https://www.ruby-forum.com/u/Blair_Z)\
**Post date:** [December 29, 2005, 9:30pm UTC](https://www.ruby-forum.com/t/why-do-plugins-use-class-eval/51335/3 "2005-12-29T21:30:53Z")

</div>

Rick O. wrote:

> > Can anybody shed any light on this?
> 
> #include is private, so you have to do:
> 
> ActiveRecord::Base.send :include, Foo::Acts::Fox

OK, thanks.

That explains the second use of it, but not the first, right, since it’s  
the  
module itself that calls acts\_as\_fox, so it can call include (or extend)  
on itself.

Regards,  
Blair

–  
Blair Z., Ph.D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subversion and Orca training and consulting  
[http://www.orcaware.com/svn/](http://www.orcaware.com/svn/)

---

<div class="post-metadata">

**Author:** ![Blair\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@Blair\_Z](https://www.ruby-forum.com/u/Blair_Z)\
**Post date:** [December 29, 2005, 9:49pm UTC](https://www.ruby-forum.com/t/why-do-plugins-use-class-eval/51335/4 "2005-12-29T21:49:03Z")

</div>

> OK, thanks.
> 
> That explains the second use of it, but not the first, right, since it’s the  
> module itself that calls acts\_as\_fox, so it can call include (or extend) on itself.

I’m not sure. I don’t think I write my plugins that way.

- checks

I guess I do! I should try taking those out and see if  
acts\_as\_versioned still works. I don’t have my black belt in  
metaprogramming…

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)
