From: "Jesús Gabriel y Galán" Date: 2011-04-11T18:36:34+09:00 Subject: Re: Copying parameters to singleton class On Mon, Apr 11, 2011 at 11:20 AM, Lars Olsson wrote: > Hi list! > > I have a metaprogramming question that is driving me mad. I though I > understood how to do it, but obviously I didn't. This is what I want > to do: > > I want to create a class method that takes a bunch of options and > returns a singleton class with those options set, > > class Opportunities >  def self.using(options) >    # Store options in singleton class variable @options and then > return singleton class >  end >  def self.options >    return @options >  end > end > > So that I can use: > > foo = Opportunities.using({:one => 1, :two: => 2, :three => 3}) > bar = Opportunities.using({:four => 4, :five: => 5, :six => 6}) > > and then > > foo.options => {:one => 1, :two: => 2, :three => 3} > bar.options => {:four => 4, :five: => 5, :six => 6} > > Please note that I don't want instances of the Opportunities class, I > want two separate classes that shares the same behavior except for > that they return different values for the Opportunities.options call. If you don't want instances of Opportunities, of which class you want instances? Where is the common behaviour of those objects defined? > This really should be possible with ruby, right? I'm not sure if I understand what you want, can you tell us why this doesn't fit your requirements: ruby-1.8.7-p334 :005 > class Opportunities ruby-1.8.7-p334 :006?> attr_reader :options ruby-1.8.7-p334 :007?> def initialize options ruby-1.8.7-p334 :008?> @options = options ruby-1.8.7-p334 :009?> end ruby-1.8.7-p334 :010?> def self.using options ruby-1.8.7-p334 :011?> new(options) ruby-1.8.7-p334 :012?> end ruby-1.8.7-p334 :013?> end => nil ruby-1.8.7-p334 :014 > foo = Opportunities.using({:a => 3, :b => 4}) => #4, :a=>3}> ruby-1.8.7-p334 :015 > foo.options => {:b=>4, :a=>3} ruby-1.8.7-p334 :016 > bar = Opportunities.using({:a => 5, :b => 10}) => #10, :a=>5}> ruby-1.8.7-p334 :017 > bar.options => {:b=>10, :a=>5} Jesus.