strange class_eval behavior

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

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

Frederick Cheung wrote: