Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to get class of BasicObject ancestor (Ruby 1.9.2)?

Reply
Thread Tools

How to get class of BasicObject ancestor (Ruby 1.9.2)?

 
 
Alexey Petrushin
Guest
Posts: n/a
 
      03-06-2011
There's no :class method on BasicObject, is there any way to get class
of it's ancestors?

class SomeUtilityClassBase < BasicObject
def clone
clone = self.class.new # <= problem here, there's no way to get
:class
...
return clone
end
end

class Config < SomeUtilityClassBase
end

a = Config.new
b = a.clone

Thanks.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Sean O'Halpin
Guest
Posts: n/a
 
      03-06-2011
On Sun, Mar 6, 2011 at 3:09 PM, Alexey Petrushin <(E-Mail Removed)> wrote:
> There's no :class method on BasicObject, is there any way to get class
> of it's ancestors?


Here's one way:

module DefineClass
def self.included(other)
other.class_eval {
define_method :class do
other
end
}
end
end

class SO < BasicObject
include :efineClass

@@counter = 0
def initialize(*a, &b)
super
@@counter += 1
end

def clone
clone = self.class.new
end

def inspect
"<#{self.class}: #{@@counter}>"
end
end

s = SO.new
p s.clone

# => <SO: 2>

(I'm not a huge fan of @@class_variables in general - it's just here
to make the point that there are two different instances.)

Regards,
Sean

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      03-06-2011
You can inherit from BlankSlate instead of BasicObject, which was
modeled after BlankSlate, and call YourClassName.reveal(:class) to
enable the class method.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      03-06-2011
...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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      03-06-2011
7stud -- wrote in post #985807:
> 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."

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Michael Edgar
Guest
Posts: n/a
 
      03-06-2011
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.

Michael Edgar
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://carboni.ca/

On Mar 6, 2011, at 4:21 PM, 7stud -- wrote:

> ...or even simpler: just open up BasicObject and monkeypatch a class=20=


> method called class(untested):
>=20
> class BasicObject
> def self.class
> self
> end
> end
>=20
> Then have at it.
>=20
> --=20
> Posted via http://www.ruby-forum.com/.
>=20



 
Reply With Quote
 
Sean O'Halpin
Guest
Posts: n/a
 
      03-06-2011
> 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 :efineClass

@@counter = 0
def initialize(*a, &b)
super
@@counter += 1
end

def clone
clone = self.class.new
end

def inspect
"<#{self.class}: #{@@counter}>"
end
end

s = SO.new
p s.clone
# => <SO: 2>

class BO < SO
end

b = BO.new
p b.clone
# => <BO: 4>

Regards,
Sean

 
Reply With Quote
 
Sean O'Halpin
Guest
Posts: n/a
 
      03-06-2011
On Sun, Mar 6, 2011 at 9:21 PM, 7stud -- <(E-Mail Removed)> wrote:
> ...or even simpler: just open up BasicObject and monkeypatch a class
> method called class(untested):

!
> class BasicObject
> =A0def self.class
> =A0 =A0self
> =A0end
> end
>
> Then have at it.


You've changed the #class method of the BasicObject /class/, not the /insta=
nce/.
So BasicObject.class will now return BasicObject (rather than Class)
and BasicObject.new.class will not work.

Regards,
Sean

 
Reply With Quote
 
Alexey Petrushin
Guest
Posts: n/a
 
      03-07-2011
Thanks for advices, good news for me is that I have small amount of
ancestors, so for now I just hardcoded it's class names.

In my case using :inherited callback magic is too over engineered
solution

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Ruby 1.9.2 Constant Lookup with BasicObject Intransition Ruby 4 08-31-2010 05:30 PM
[BUG] Ruby 1.9.0: possible bug when subclassing BasicObject Michael Fellinger Ruby 3 12-27-2007 04:05 PM
ancestor class' __init__ doesn't call other methods Luis P. Mendes Python 3 09-15-2006 10:10 PM
Determining a class's ancestor easily without instances Wes Gamble Ruby 3 07-12-2006 06:55 PM



Advertisments