# strange class\_eval behavior

**URL:** <https://discuss.rubyonrails.org/t/strange-class-eval-behavior/49692>\
**Category:** rubyonrails-talk\
**Created:** [February 25, 2010, 6:28pm UTC](https://discuss.rubyonrails.org/t/strange-class-eval-behavior/49692 "2010-02-25T18:28:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [February 25, 2010, 6:28pm UTC](https://discuss.rubyonrails.org/t/strange-class-eval-behavior/49692/1 "2010-02-25T18:28:35Z")

</div>

hi again, all. I have the following plugin included in my active record model. What i'd like to do, is define a method inside class\_eval which returns the options has that was passed into it via the acts\_as\_item :foo=\>:bar in my model.

What i'm finding odd is that the self.foo method can access the config hash when printing it, but not when i try to return it.

puts "OK FOO: #{config[:asset\_root]}" - outputs ok, whereas the return config raises a method or variable not found error.

Any ideas? best, paul.

&nbsp;&nbsp;&nbsp;&nbsp;module ClassMethods &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def acts\_as\_item(options = {}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include InstanceMethods

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config = {:asset\_root=\>"SOMEPATH"} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config.update(options) if options.is\_a?(Hash)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class\_eval \<\<-EOE &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.foo &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "OK FOO: #{config[:asset\_root]}" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return config &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EOE &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [February 25, 2010, 10:13pm UTC](https://discuss.rubyonrails.org/t/strange-class-eval-behavior/49692/2 "2010-02-25T22:13:25Z")

</div>

> hi again, all. I have the following plugin included in my active record model. What i'd like to do, is define a method inside class\_eval which returns the options has that was passed into it via the acts\_as\_item :foo=\>:bar in my model.
> 
> What i'm finding odd is that the self.foo method can access the config hash when printing it, but not when i try to return it.

Two very different things are happening: with the puts config is being resolved just before class\_eval is called: the string is interpolated and the result passed to class\_eval. The return statement on the other hand is evaluated much later - when the foo method is actually called. config is no longer in scope and so you get an error.

Fred

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [February 26, 2010, 12:03pm UTC](https://discuss.rubyonrails.org/t/strange-class-eval-behavior/49692/3 "2010-02-26T12:03:32Z")

</div>

Frederick Cheung wrote:
