You can inherit from BlankSlate instead of BasicObject, which was
modeled after BlankSlate, and call YourClassName.reveal(:class) to
enable the class method.
If you inherit from BlankSlate instead of BasicObject. The advantage
of doing that is that BlankSlate has a class method called reveal(),
which you can use to incrementally roll back your BlankSlate. In your
case, you would call YourClassName.reveal(:class) to
enable the class method.
That last line should be:
"enable the :class instance method"
and the first line should read:
"You can inherit from BlankSlate instead of BasicObject."
BasicObject as a singleton already has the class method (and it returns Class, as expected).
You meant to suggest something like this:
class BasicObject
def class
BasicObject
end
end
But that would make any subclass of BasicObject that doesn't override this #class definition
would return BasicObject, which would be odd. I'm not sure of a pure-ruby way to write #class,
it's definitely written in C for the Object class.
...or even simpler: just open up BasicObject and monkeypatch a class
method called class(untested):
!
class BasicObject
def self.class
self
end
end
Then have at it.
You've changed the #class method of the BasicObject /class/, not the /instance/.
So BasicObject.class will now return BasicObject (rather than Class)
and BasicObject.new.class will not work.
Regards,
Sean
···
On Sun, Mar 6, 2011 at 9:21 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
I'm not sure of a pure-ruby way to write #class,
it's definitely written in C for the Object class.
Here's another attempt (this time handling inheritance):
module DefineClass
def self.define_class(klass)
klass.class_eval {
define_method :class do
klass
end
}
end
def self.extended(other)
define_class(other)
end
def inherited(other)
DefineClass.define_class(other)
end
end
class SO < BasicObject
extend ::DefineClass
@@counter = 0
def initialize(*a, &b)
super
@@counter += 1
end
def clone
clone = self.class.new
end
def inspect
"<#{self.class}: #{@@counter}>"
end
end