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

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?

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.

···

On Nov 16, 2007 8:47 AM, Voroztsov Artem <artem.voroztsov@gmail.com> wrote:

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?

--
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

···

2007/11/16, Rick DeNatale <rick.denatale@gmail.com>:

On Nov 16, 2007 8:47 AM, Voroztsov Artem <artem.voroztsov@gmail.com> wrote:
> 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.

--
Rick DeNatale

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

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

···

On Nov 16, 2007 5:56 PM, Voroztsov Artem <artem.voroztsov@gmail.com> wrote:

2007/11/16, Rick DeNatale <rick.denatale@gmail.com>:

> On Nov 16, 2007 8:47 AM, Voroztsov Artem <artem.voroztsov@gmail.com> wrote:
> > 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.
>
> --
> 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