From: Calvin Bornhofen Date: 2012-09-27T05:14:46+09:00 Subject: Re: Adding method on module class to yield and allow setting of attributes? 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 @. > module MyModule > class << self > attr_accessor :foo > alias_method :foo?, :foo > # def configure(&block); yield; end # doesn't work > end > end > > Thanks in advance for any advice. I'd rather not use any > Active*/Rails-specific method and don't want to use Struct, OpenStruct, > 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