From: Gary Weaver Date: 2012-09-27T06:15:16+09:00 Subject: Re: Adding method on module class to yield and allow setting of attributes? Calvin B. wrote in post #1077674: > Hi, > > first off, you're not that wrong with your idea. There are two things > which need to be changed. >> MyModule.configure do >> foo = false >> end > The local variable foo is just that - a local variable. You want it to > be a class instance variable, so you need to precede it with an @. > >> etc. I just want to be able to call the methods created by the >> attr_accessor I used to set and get attributes. The reasons I'm doing >> this to a module and not a class is it is convenient since it is the >> module that my Gem's classes use. > You don't need to use an external library. The thing you did wrong in > your configure method is that it executes the block in the scope of it's > creation, so even if you use the @, it is not a variable in the > class/module scope. So what you need to do is call class_eval with the > block as a parameter: > def configure( &block ); class_eval █ end Calvin, Thanks! That is even shorter. It is nice to have both options available, so when do: module MyModule class << self attr_accessor :foo alias_method :foo?, :foo def configure(&blk) class_eval(&blk) end end end can do either: MyModule.configure do @foo = false def self.time; Time.now; end end or MyModule.configure do self.foo = false def self.time; Time.now; end end That is so awesome! Thanks for everyone's help! -- Posted via http://www.ruby-forum.com/.