A
Avdi Grimm
Okay, I'm trying to mock up a class object. It needs to do two
things:
1. Respond to #new with a object *I* provide, rather than a new
instance of the class; and
2. Contain a constant, such that code containing 'MyMockClass::FOO'
will work.
I can think of a naive implementation:
class MyMockClass
FOO="some value"
def self.set_instance(inst)
@instance=inst
end
def self.new(*)
return @instance
end
end
I'm wondering, though, is there a way to do this inline with
metaprogramming, and avoid defining the set_instance method? I've
tried something like this:
my_instance=...
Class.new :MyMockClass {
const_set :FOO, "some value"
define_methodnew) do |*|
return my_instance
end
}
But that doesn't work, because the define_method is creating an
instance method, not a class method. I've also tried to use a regular
old object, instead of a class object, but then the constant (FOO)
doesn't work. Any ideas?
things:
1. Respond to #new with a object *I* provide, rather than a new
instance of the class; and
2. Contain a constant, such that code containing 'MyMockClass::FOO'
will work.
I can think of a naive implementation:
class MyMockClass
FOO="some value"
def self.set_instance(inst)
@instance=inst
end
def self.new(*)
return @instance
end
end
I'm wondering, though, is there a way to do this inline with
metaprogramming, and avoid defining the set_instance method? I've
tried something like this:
my_instance=...
Class.new :MyMockClass {
const_set :FOO, "some value"
define_methodnew) do |*|
return my_instance
end
}
But that doesn't work, because the define_method is creating an
instance method, not a class method. I've also tried to use a regular
old object, instead of a class object, but then the constant (FOO)
doesn't work. Any ideas?