# Strange class\_eval behavior

**URL:** <https://www.ruby-forum.com/t/strange-class-eval-behavior/184157>\
**Category:** Rails\
**Created:** [February 25, 2010, 7:28pm UTC](https://www.ruby-forum.com/t/strange-class-eval-behavior/184157 "2010-02-25T19:28:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![extempore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/6bbea6/32.png) [@extempore](https://www.ruby-forum.com/u/extempore)\
**Post date:** [February 25, 2010, 7:28pm UTC](https://www.ruby-forum.com/t/strange-class-eval-behavior/184157/1 "2010-02-25T19: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.

```
module ClassMethods
  def acts_as_item(options = {})
    include InstanceMethods

    config = {:asset_root=>"SOMEPATH"}
    config.update(options) if options.is_a?(Hash)

    class_eval <<-EOE
      def self.foo
        puts "OK FOO: #{config[:asset_root]}"
        return config
      end
    EOE
  end
end
```

---

<div class="post-metadata">

**Author:** ![extempore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/6bbea6/32.png) [@extempore](https://www.ruby-forum.com/u/extempore)\
**Post date:** [February 25, 2010, 11:14pm UTC](https://www.ruby-forum.com/t/strange-class-eval-behavior/184157/2 "2010-02-25T23:14:42Z")

</div>

On Feb 25, 6:28 pm, Paul PH [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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:** ![extempore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/6bbea6/32.png) [@extempore](https://www.ruby-forum.com/u/extempore)\
**Post date:** [February 26, 2010, 1:03pm UTC](https://www.ruby-forum.com/t/strange-class-eval-behavior/184157/3 "2010-02-26T13:03:32Z")

</div>

Frederick C. wrote:

> On Feb 25, 6:28ï¿½pm, Paul PH [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 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

Ah, I see - thanks Fred.  
I understand the variable is resolved earlier to do the string  
interpolation. I do find it odd that it is put into scope for that, but  
not for returning it.

Thanks, again.  
Paul.
