From: Gary Weaver Date: 2012-09-27T06:09:55+09:00 Subject: Re: Adding method on module class to yield and allow setting of attributes? "Jesús Gabriel y Galán" wrote in post #1077681: > On Wed, Sep 26, 2012 at 10:41 PM, Gary Weaver > wrote: >> >>> You could do this: >>> >> That is helpful, but I have a way to do a hash easily already via >> exactly like that, but originally I thought this would be doable, and am >> just kind of stuck. Ilia had suggested ActiveSupport::Configurable on >> the list but it requires config to be sent in as a parameter and I was >> hoping to allow method definition and not have to specify the >> config.varname = ... > > I don't know if my previous message got through. If you can handle > some changes in the syntax, using self. before the attributes and the > method names should work if you do a class_eval: > > 1.9.2p290 :013 > module MyModule > 1.9.2p290 :014?> class << self > 1.9.2p290 :015?> attr_accessor :foo > 1.9.2p290 :016?> alias_method :foo?, :foo > 1.9.2p290 :017?> def configure(&blk) > 1.9.2p290 :018?> class_eval(&blk) > 1.9.2p290 :019?> end > 1.9.2p290 :020?> end > 1.9.2p290 :021?> end > => nil > 1.9.2p290 :022 > MyModule.configure do > 1.9.2p290 :023 > def self.test; puts "test"; end > 1.9.2p290 :024?> end > => nil > 1.9.2p290 :025 > MyModule.test > test > => nil > > > > Jesus. Thank you, Jesus! That's exactly what I was looking for! module MyModule class << self attr_accessor :foo, :time alias_method :foo?, :foo def configure(&blk) class_eval(&blk) end end end MyModule.foo # nil MyModule.time # nil MyModule.configure do self.foo = false def self.time; Time.now; end end MyModule.foo # false MyModule.time # current date and time Gary -- Posted via http://www.ruby-forum.com/.