Reliable way of finding Objects Class?

Hi,

is there a reliable way to find the Object’s Class. I know about
class and type, but what happens if they are overridden?

how do you find it then?

for instance how to find the class at runtime for the following :

class Obj
def class
"Not My Class"
end

def type
	"don't look here for it"
end

end

I am sure it is very simple, I just can’t seem to find it.

Any hints would be greatly appreciated.

Thanks,

Walt

···

Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : walter@mwsewall.com
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284


Hello –

Hi,

is there a reliable way to find the Object’s Class. I know about
class and type, but what happens if they are overridden?

how do you find it then?

for instance how to find the class at runtime for the following :

class Obj
def class
“Not My Class”
end

def type
“don’t look here for it”
end
end

Note that Object#type is on the way out:

$ ruby -ve ‘1.type’
ruby 1.8.0 (2003-06-23) [i686-linux]
-e:1: warning: Object#type is deprecated; use Object#class

As for finding an object’s class without Object#class, I guess you
could do something like:

class Obj; def class; “Go away!”; end; end
=> nil
o = Obj.new
=> #Obj:0x401e98c8
a = ; ObjectSpace.each_object(Class) {|c| a << c if o.is_a?(c)}
=> 346
a[-1]
=> Obj

But ObjectSpace.each_object can also be overridden. There’s a finite
number of ways to get this information, none of which as far as I know
is override-proof.

David

···

On Thu, 17 Jul 2003 walter@mwsewall.com wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi,

is there a reliable way to find the Object’s Class. I know about
class and type, but what happens if they are overridden?

This is not perfect but working way:

c = Object.instance_method(:class)
class Foo
def class
nil
end
end
f = Foo.new
p f.class # => nil
p c.bind(f).call # => Foo
matz.

···

In message “reliable way of finding Objects Class?” on 03/07/17, walter@mwsewall.com walter@mwsewall.com writes:

walter@mwsewall.com wrote:

is there a reliable way to find the Object’s Class. I know about
class and type, but what happens if they are overridden?

You are technically correct that someone /could/ override a class’
class() method, but I think it’s pretty well understood that such a
person would need to be kil^H^H^H discouraged from doing that :wink:

So I believe that it is reliable to use the class() method to determine
an object’s class. Have you actually run across a class whose class()
method has been overridden?

“is_a?” matches superclasses as well - “instance_of?” would be better here I
think.

But like David says, if you shoot off all your feet, you won’t have anything
left to stand on :slight_smile:

Cheers,

Brian.

···

On Thu, Jul 17, 2003 at 11:08:00PM +0900, dblack@superlink.net wrote:

As for finding an object’s class without Object#class, I guess you
could do something like:

class Obj; def class; “Go away!”; end; end
=> nil
o = Obj.new
=> #Obj:0x401e98c8
a = ; ObjectSpace.each_object(Class) {|c| a << c if o.is_a?(c)}
=> 346
a[-1]
=> Obj

My problem is in a Persistence Framework that I have, and with
dynamically generated classes from a database schema ( and in a
separate but similar issue with dynamically created classes from a
resultSet).

I need to find the objects class to get the correct database mapping,
but the objects are (in this case) dynamically created from a
database schema, and several of those overide class.

I was hoping to be able to find an objects class from a different
object, perhaps Class. Something like Class.class(obj), that way I
would not have to worry about naming clashes.

The ObjectSpace would work, but I am worried about performance.

Thanks,

Walt

···

Hello --

On Thu, 17 Jul 2003 walter@mwsewall.com wrote:

> Hi,
>
> is there a reliable way to find the Object's Class. I know about
> class and type, but what happens if they are overridden?
>
> how do you find it then?
>
> for instance how to find the class at runtime for the following :
>
> class Obj
> def class
> "Not My Class"
> end
>
> def type
> "don't look here for it"
> end
> end

Note that Object#type is on the way out:

  $ ruby -ve '1.type'
  ruby 1.8.0 (2003-06-23) [i686-linux]
  -e:1: warning: Object#type is deprecated; use Object#class

As for finding an object's class without Object#class, I guess you
could do something like:

  >> class Obj; def class; "Go away!"; end; end
  => nil
  >> o = Obj.new
  => #<Obj:0x401e98c8>
  >> a = ; ObjectSpace.each_object(Class) {|c| a << c if o.is_a?(c)}
  => 346 >> a[-1] => Obj

But ObjectSpace.each_object can also be overridden. There's a finite
number of ways to get this information, none of which as far as I know
is override-proof.

David

--
David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

*****************************************************
Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : walter@mwsewall.com
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284
*****************************************************

Perfect or not,

It will take care of my problem.

Thanks,

Walt

···

Hi,

In message “reliable way of finding Objects Class?”
on 03/07/17, walter@mwsewall.com walter@mwsewall.com writes: |

is there a reliable way to find the Object’s Class. I know about
class and type, but what happens if they are overridden?

This is not perfect but working way:

c = Object.instance_method(:class)
class Foo
def class
nil
end
end
f = Foo.new
p f.class # => nil
p c.bind(f).call # => Foo
matz.


Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : walter@mwsewall.com
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284


is there a reliable way to find the Object’s Class. I know about
class and type, but what happens if they are overridden?

You are technically correct that someone /could/ override a class’
class() method, but I think it’s pretty well understood that such a
person would need to be kil^H^H^H discouraged from doing that :wink:

So I believe that it is reliable to use the class() method to determine
an object’s class. Have you actually run across a class whose class()
method has been overridden?

I tend to agree. Too, this is an instance, if you will, of a larger
question I see with somewhat regularity on this list. Here’s another
instance of the question, see if you can pick out the “class”…

“Is there a reliable way to add numbers? I know about “+”, but what happens
if it’s overridden?”

Sometimes, you just have to trust that people Don’t Do Inordinately Stupid
Things. And have appropriate unit tests.

Hi,

My problem is in a Persistence Framework that I have, and with
dynamically generated classes from a database schema ( and in a
separate but similar issue with dynamically created classes from a
resultSet).

I need to find the objects class to get the correct database mapping,
but the objects are (in this case) dynamically created from a
database schema, and several of those overide class.

I was hoping to be able to find an objects class from a different
object, perhaps Class. Something like Class.class(obj), that way I
would not have to worry about naming clashes.

How about maybe aliasing the ‘class’ method to something
you can presume nothing could ever clash with?

Silly example:

class Object
alias no_way_in_doodely_heckfire_to_have_name_clash_with_class class
end

puts 2.no_way_in_doodely_heckfire_to_have_name_clash_with_class
=> Fixnum

?

Regards,

Bill

···

From: walter@mwsewall.com