S
Steve Midgley
Hey,
I've been working on a weird little problem that I thought people on
this list might have a quick answer for (though the answer may be
"impossible!"):
[code starts]
class Test; end
test = Test.new
class << test
def new_method
puts "new method is accessible!"
end
end
test.new_method # works
test1 = test.dup # works but creates class from original class not
singleton
test2 = class << test; self.new; end; # blows up with "can't create
instance of virtual class (TypeError)"
# neither of these will therefore work
test1.new_method
test2.new_method
[code ends]
Basically, I have a class "Test" and I create an instance of it. I
modify the instance's class definition (creating a singleton class
definition).
Now what I really want is to create a second instance of the newly
created singleton class. It appears to be impossible. A guess the name
singleton implies as much. But with all of Ruby's tricks, I thought
there'd be a way to create a new instance of a singleton.. Probably
there is but I don't see it.
Hmm..
Steve
p.s. I've simplified the use-case way down - there are external reasons
why I want to modify the singleton and create an instance of it, rather
than modifying the parent class (or at least I think there are)..
I've been working on a weird little problem that I thought people on
this list might have a quick answer for (though the answer may be
"impossible!"):
[code starts]
class Test; end
test = Test.new
class << test
def new_method
puts "new method is accessible!"
end
end
test.new_method # works
test1 = test.dup # works but creates class from original class not
singleton
test2 = class << test; self.new; end; # blows up with "can't create
instance of virtual class (TypeError)"
# neither of these will therefore work
test1.new_method
test2.new_method
[code ends]
Basically, I have a class "Test" and I create an instance of it. I
modify the instance's class definition (creating a singleton class
definition).
Now what I really want is to create a second instance of the newly
created singleton class. It appears to be impossible. A guess the name
singleton implies as much. But with all of Ruby's tricks, I thought
there'd be a way to create a new instance of a singleton.. Probably
there is but I don't see it.
Hmm..
Steve
p.s. I've simplified the use-case way down - there are external reasons
why I want to modify the singleton and create an instance of it, rather
than modifying the parent class (or at least I think there are)..