On Feb 25, 6:28 pm, Paul PH <li...@ruby-forum.com> 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

> 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
> --
> Posted viahttp://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to