What is the correct way to check for existence of virtual class?

V

Voroztsov Artem

One can write:

class Object
def singleton_class
class <<self; self; end rescue nil
end
end

puts "aaa".singleton_class
puts 8.singleton_class

Is it the assumed way to do the job?

Is there any special method in ruby 1.9?
 
R

Rick DeNatale

One can write:

class Object
def singleton_class
class <<self; self; end rescue nil
end
end

puts "aaa".singleton_class
puts 8.singleton_class

Is it the assumed way to do the job?

There's a bit of a Heisenberg problem here, in that the act of
measuring alters the result.

Singleton instance classes aren't created until they are needed, but
one of the things which creates them is class <<x;end so you're really
not checking whether or not the singleton class already exists this
way.

Another way would be

!var.singleton_methods.empty?

BTW, your 8.singleton_class will fail, Fixnums can't have singleton classes.
 
V

Voroztsov Artem

2007/11/16 said:
There's a bit of a Heisenberg problem here, in that the act of
measuring alters the result.

Singleton instance classes aren't created until they are needed, but
one of the things which creates them is class <<x;end so you're really
not checking whether or not the singleton class already exists this
way.

Another way would be

!var.singleton_methods.empty?

BTW, your 8.singleton_class will fail, Fixnums can't have singleton classes.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Thanks!

I see.

In fact I want to create singleton class if it is possible.

I guess checking if singleton class already exists should be tricky
coding or impossible.
Let's look at the code:

class Object
def singleton_class
class <<self; self; end rescue nil
end
end

["abc", 8].each do |a|
puts "\nObject: #{a}"

puts !a.singleton_methods.empty?

puts a.singleton_class

puts !a.singleton_methods.empty?
end


Output is

Object: abc
false
#<Class:#<String:0x2b28c50>>
false

Object: 8
false
nil
false


So it looks like expression

!a.singleton_methods.empty?

can miss existence of singleton class.

But in fact these two problems have pragmatic sense:
1) Get singleton class if it is possible
2) Check if objects has singleton methods

While this one -- not :
3) Check if singleton class exists
(now I understand that there is no use case for it)

Artem
 
S

Sean O'Halpin

2007/11/16 said:
There's a bit of a Heisenberg problem here, in that the act of
measuring alters the result.

Singleton instance classes aren't created until they are needed, but
one of the things which creates them is class <<x;end so you're really
not checking whether or not the singleton class already exists this
way.

Another way would be

!var.singleton_methods.empty?

BTW, your 8.singleton_class will fail, Fixnums can't have singleton classes.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Thanks!

I see.

In fact I want to create singleton class if it is possible.

I guess checking if singleton class already exists should be tricky
coding or impossible.
Let's look at the code:

class Object
def singleton_class
class <<self; self; end rescue nil
end
end

["abc", 8].each do |a|
puts "\nObject: #{a}"

puts !a.singleton_methods.empty?

puts a.singleton_class

puts !a.singleton_methods.empty?
end


Output is

Object: abc
false
#<Class:#<String:0x2b28c50>>
false

Object: 8
false
nil
false


So it looks like expression

!a.singleton_methods.empty?

can miss existence of singleton class.

But in fact these two problems have pragmatic sense:
1) Get singleton class if it is possible
2) Check if objects has singleton methods

While this one -- not :
3) Check if singleton class exists
(now I understand that there is no use case for it)

Artem
FWIW, these are my experiments with trying to find a way to detect if
I'm currently executing within a singleton scope:

def in_singleton?
if self.respond_to?:)superclass)
if superclass && superclass.superclass
# the mystery of that which is its own parent <#Class:Class> (&
grandparent, ad infinitum)
superclass.superclass.superclass == superclass.superclass
else
false
end
else
false
end
end
public :in_singleton?

class O
in_singleton? # => false
end
class O
self.in_singleton? # => false
end
class O
class << self
in_singleton? # => true
end
end

class P
end

o = P.new
o.in_singleton? # => false
class << o
in_singleton? # => true
end
o.in_singleton? # => false
nil.in_singleton? # => false
NilClass.in_singleton? # => false
Object.in_singleton? # => false
Class.in_singleton? # => false
class Class
in_singleton? # => false
class << self
in_singleton? # => true
end
end
class Qux
in_singleton? # => false
class << self
in_singleton? # => true
end
end
# but...
class Object
in_singleton? # => false
class << self
in_singleton? # => false
end
end

Regards,
Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top