dblack wrote:
I think the idea is for the object to have its own class, so if that
class ceased to be its own class, there would have to be some new
construct that was its own class, and you’d be back where you started
So I don’t think it’s really possible in terms of the logic of
the model, and I can’t think of any way to do it.
Is it possible you could use modules to bring about the kind of thing
you’re looking for?
Thanks David - I think I understand what you’re saying… I’d happily
settle for a copy of the instance’s singleton class, but maybe I’m
missing your point?
Another way to describe what I want to do:
- create a class “Test”
- create an instance of Test: “test1”
- modify test1’s class definition, creating a singleton class
- Somehow “promote” or copy this singleton class to be a full fledged
class as “Test2”
- Create an instance of “Test2” as “test2”
Obviously #4 is the place where I’m stuck. B/c I’m stuck, I am (as you
suggest) using module mix-in’s to solve the problem, but it’s less
clean due to my context in the code… It’ll work, but not as nicely as
if there were a method such as:
Test2 = test1.dup_singleton_class
test2 = Test2.new
I can’t see why Ruby would object inherently to copying all the class
methods of a singleton into a new class object (which is all I want -
just a template of the singleton class’s methods - no class variables
or whatnot).
Thanks for the insight,
Steve
On Thu, 15 Feb 2007, Steve M. wrote:
as “Test2”
5) Create an instance of “Test2” as “test2”
harp:~ > cat a.rb
require ‘rubygems’
require ‘prototype’
1)
class Test; end
2)
test1 = Test.new
3,4)
Test2 = Class.new(test1.class){
def new_method
p ‘no singleton class is needed’
end
}
5)
test2 = Test2.new
test2.new_method
harp:~ > ruby a.rb
“no singleton class is needed”
of the singleton class’s methods - no class variables or whatnot).
you have to have the the class variables if you have class methods.
otherwise this
class C
@x = 42
def self.x() @x end
end
won’t work
in strongly suspect there is an easier way to skin your cat. can you
tell us,
not what you are trying to do, but why?
-a
On Thu, 15 Feb 2007 [email protected] wrote:
Is it possible you could use modules to bring about the kind of thing
- modify test1’s class definition, creating a singleton class
5)
test2 = Test2.new
test2.new_method
harp:~ > ruby a.rb
“no singleton class is needed”
and, if it is
harp:~ > cat a.rb
require ‘rubygems’
require ‘prototype’
1)
class Test; end
2)
test1 = Test.new
3)
m = Module.new{
def new_method
p ‘if a singleton class is needed’
end
}
(class << test1; self; end).module_eval{ include m }
much easier - no singleton class needed here either
test1.extend m
4)
Test2 = Class.new(test1.class){ include m }
5)
test2 = Test2.new
test2.new_method
harp:~ > ruby a.rb
“if a singleton class is needed”
-a
Hi –
On Thu, 15 Feb 2007, [email protected] wrote:
harp:~ > cat a.rb
require ‘rubygems’
require ‘prototype’
Are you sure you need those for your example?
def self.x() @x end
end
won’t work
That’s not a class variable, though – but I’m a bit confused by
Steve’s reference to class methods. Steve, why are you referring to
class methods?
David