From: 7stud -- Date: 2011-04-12T11:48:38+09:00 Subject: Re: Copying parameters to singleton class ...but then you could do this: module ClassFactory def ClassFactory.included(includer) includer.extend(ClassMethods) end module ClassMethods def using(hash) Class.new do @options = hash class << self attr_reader :options end end end end end class Opportunities include ClassFactory end Foo = Opportunities.using({one: 1, two: 2, three: 3}) Bar = Opportunities.using({four: 4, five: 5, six: 6}) p Foo.options p Bar.options --output:-- {:one=>1, :two=>2, :three=>3} {:four=>4, :five=>5, :six=>6} I really wanted to write that like below, but it doesn't work?? module ClassFactory def ClassFactory.included(includer) includer.extend(ClassMethods) end module ClassMethods def using(hash) Class.new do @options = hash end end def options @options end end end class Opportunities include ClassFactory end Foo = Opportunities.using({one: 1, two: 2, three: 3}) Bar = Opportunities.using({four: 4, five: 5, six: 6}) p Foo.options p Bar.options --output:-- prog.rb:28:in `
': undefined method `options' for Foo:Class (NoMethodError) -- Posted via http://www.ruby-forum.com/.