From: Robert Klemme Date: 2011-04-12T15:53:56+09:00 Subject: Re: Copying parameters to singleton class On Tue, Apr 12, 2011 at 4:05 AM, 7stud -- wrote: > Robert K. wrote in post #992088: >> >> Choose to your liking. :-) >> > > Hey Robert K, > > I read your factory pattern thread the other day, and I wonder if that > pattern would apply here?  After all, producing several anonymous > instances of Class is akin to producing instances of various classes. > Something like this: > > class ClassFactory >  def self.using(hash) >    Class.new do >     @options = hash > >       class << self >         attr_reader :options >       end > >     end >  end > end Well, basically you just exchanged Opportunities with ClassFactory compared to one of my examples. Actually method #using *is* a factory method, no matter what class or instance it sits in. > foo = Opportunities.using({one: 1, two: 2, three: 3}) > bar = Opportunities.using({four: 4, five: 5, six: 6}) I don't think this can work - at least there's something missing. Did you mean to make Opportunities inherit ClassFactory? But IMHO there is no point in splitting this up. Please note also that I made new classes inherit Opportunities in order for inheritance to work. > p foo.options > p bar.options > > --output:-- > {:one=>1, :two=>2, :three=>3} > {:four=>4, :five=>5, :six=>6} > > > Hey Lars Olsson, > > Check out the shorter syntax for hashes when your keys are symbols. > > foo = Opportunities.using({one: 1, two: 2, three: 3}) > bar = Opportunities.using({four: 4, five: 5, six: 6}) If we start shortening it, we should do foo = Opportunities.using(one: 1, two: 2, three: 3) i.e. get rid of the superfluous pair of curly braces. :-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/