Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2618

Re: Copying parameters to singleton class

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.ruby
Subject Re: Copying parameters to singleton class
Date 2011-04-11 04:34 -0500
Organization Service de news de lacave.net
Message-ID <BANLkTimh8S9MGSZ+GEteAM70Hs4-bUsDLg@mail.gmail.com> (permalink)
References <e7b941e2-199e-423b-97bb-7fee63905659@hg8g2000vbb.googlegroups.com>

Show all headers | View raw


On Mon, Apr 11, 2011 at 11:20 AM, Lars Olsson <lasso@lassoweb.se> 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.
>
> This really should be possible with ruby, right?

Right.  You can use a closure like this:

class Opportunities
  def self.using(opts)
    opts.freeze
    cl = Class.new self # so we can define instance methods
    class<<cl;self;end.send(:define_method, :options) {opts}
    cl
  end

  # inherited by all
  def xyz
  end
end


irb(main):019:0> foo = Opportunities.using(:one => 1, :two => 2, :three => 3)
=> #<Class:0x10958c78>
irb(main):020:0> bar = Opportunities.using(:four => 4, :five => 5, :six => 6)
=> #<Class:0x1095f7f8>
irb(main):021:0> foo.options
=> {:one=>1, :two=>2, :three=>3}
irb(main):022:0> bar.options
=> {:four=>4, :five=>5, :six=>6}
irb(main):023:0> f = foo.new
=> #<#<Class:0x10958c78>:0x109672dc>
irb(main):024:0> f.class.options
=> {:one=>1, :two=>2, :three=>3}
irb(main):025:0> f.class.ancestors
=> [#<Class:0x10958c78>, Opportunities, Object, Kernel, BasicObject]

Or you can use a member

class Opportunities
  def self.using(opts)
    cl = Class.new self

    class<<cl
      attr_reader :options
    end

    cl.instance_variable_set '@options', opts.freeze

    cl
  end
end

or this way

class Opportunities
  class <<self
    attr_reader :options
  end

  def self.using(opts)
    cl = Class.new self
    cl.instance_variable_set '@options', opts.freeze
    cl
  end
end

or this way

class Opportunities
  class <<self
    attr_reader :options
  end

  def self.using(opts)
    Class.new(self).tap do |cl|
      cl.instance_variable_set '@options', opts.freeze
    end
  end
end


Choose to your liking. :-)

Cheers

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Copying parameters to singleton class Lars Olsson <lasso@lassoweb.se> - 2011-04-11 02:19 -0700
  Re: Copying parameters to singleton class Robert Klemme <shortcutter@googlemail.com> - 2011-04-11 04:34 -0500
    Re: Copying parameters to singleton class 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-11 21:21 -0500
    Re: Copying parameters to singleton class 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-11 21:05 -0500
      Re: Copying parameters to singleton class Robert Klemme <shortcutter@googlemail.com> - 2011-04-12 01:53 -0500
        Re: Copying parameters to singleton class 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-12 13:31 -0500
    Re: Copying parameters to singleton class 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-12 19:08 -0500
  Re: Copying parameters to singleton class Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-11 04:36 -0500
    Re: Copying parameters to singleton class Lars Olsson <lasso@lassoweb.se> - 2011-04-11 02:53 -0700
  Re: Copying parameters to singleton class 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-11 21:48 -0500
    Re: Copying parameters to singleton class Robert Klemme <shortcutter@googlemail.com> - 2011-04-12 02:00 -0500
      Re: Copying parameters to singleton class Lars Olsson <lasso@lassoweb.se> - 2011-04-12 01:25 -0700
        Re: Copying parameters to singleton class Robert Klemme <shortcutter@googlemail.com> - 2011-04-12 03:51 -0500
        Re: Copying parameters to singleton class 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-12 13:14 -0500
          Re: Copying parameters to singleton class 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-12 13:38 -0500
      Re: Copying parameters to singleton class 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-12 13:43 -0500
  Re: Copying parameters to singleton class 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-12 19:29 -0500
    Re: Copying parameters to singleton class Lars Olsson <lasso@lassoweb.se> - 2011-04-13 01:49 -0700
  Re: Copying parameters to singleton class 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-13 19:32 -0500
  Re: Copying parameters to singleton class 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-13 19:49 -0500
    Re: Copying parameters to singleton class 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-13 20:09 -0500
      Re: Copying parameters to singleton class Lars Olsson <lasso@lassoweb.se> - 2011-04-14 01:33 -0700
        Re: Copying parameters to singleton class 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-14 12:07 -0500

csiph-web