class MyObject
def class
end
def type
end
def hello
puts "HELLO!"
end
end
a = MyObject.new
Question: How to determine type of ‘a’ object?
class MyObject
def class
end
def type
end
def hello
puts "HELLO!"
end
end
a = MyObject.new
Question: How to determine type of ‘a’ object?
Szymon Drejewicz wrote:
class MyObject
def class
end
def type
end
def hello
puts “HELLO!”
end
enda = MyObject.new
Question: How to determine type of ‘a’ object?
Quick hack:
def find_class_of( obj )
potentials =
ObjectSpace.each_object(Class){|c|
potentials << c if obj.kind_of? c
}
potentials.sort!.first
end
It will probably break for some wierd, contrived classhierarchy, but I
can’t think of one right now…
–
([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_/ö____/ (_engineering.discipline=Computer::Technology)
Hi,
At Sat, 15 Mar 2003 01:21:45 +0900, Szymon Drejewicz wrote:
a = MyObject.new
p(class << a; superclass; end)
–
Nobu Nakada
Very nice.
Is there still a way to get the class if we do this even more crazy?
class MyObject
def class
end
def type
end
def kind_of?(*args)
end
def self.===(*args)
end
class <<self
alias :old_new :new
private :old_new
end
def self.new(*args)
obj = old_new(*args)
def obj.superclass
end
end
end
a = MyObject.new
Or put another way, just how deep can we bury the actual type?
nobu.nokada@softhome.net wrote:
Szymon Drejewicz wrote:
a = MyObject.new
p(class << a; superclass; end)
–
([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_/ö____/ (_engineering.discipline=Computer::Technology)
Hi,
Szymon Drejewicz wrote:
a = MyObject.new
p(class << a; superclass; end)
Very nice.
Is there still a way to get the class if we do this even more crazy?
class MyObject
def self.new(*args)
obj = old_new(*args)
def obj.superclass
end
end
end
This class makes nothing.
class MyObject
def class
end
def type
end
def kind_of?(*args)
end
def self.===(*args)
end
def self.superclass
end
def self.ancestors
end
end
p a = MyObject.new
p(class << a; c=nil;ObjectSpace.each_object(Class) {|c|break if c>=self}; c; end)
However, you can override MyObject.>= and so on, of course.
At Sat, 15 Mar 2003 02:22:02 +0900, Kent Dahl wrote:
–
Nobu Nakada