What (class << self; self; end) means

Hello,

This is my first post at ruby-talk
I'm a some year exprience of rubyist without deep understanding :frowning:
Now I'm trying to understand more about meta programming.

I browsed other people's sources and have a question.

What does (class << self; self; end) means ?

I did an experience like this.

#!/usr/bin/ruby
class Cat
聽聽聽def self.mew
聽聽聽聽聽p self
聽聽聽聽聽p (class << self; self; end)
聽聽聽end
end
Cat.mew

The output was

> Cat
> #<Class:Cat>

I think the latter( #<Class:Cat> ) means Class singleton object.
Moreover, from my understanding I've been thinking that the former also means Class singleton object.

Could you please tell the difference between self and (class << self; self; end) in the context ?

Thank you,

Toshi Umehara now @Tokyo, soon @Atlanta

class << X

opens the singleton class of object X, whatever that is. X could be a
normal object or a class or whatever. Inside that scope, "self" means
the object that is the singleton class of X.

So,

class Cat
  def self.mew
    class << self

opens the scope of the singleton class of the object that happens to
be self at that point. As you are in a class instance method, self is
the object Cat (the class object, remember that classes are objects
too). So,

class Cat
  def self.mew # this creates a class instance method
    p self # here, self is the Cat class object (classes are objects too)
    class << self
      self # this self is the singleton class of the previous line
self, which is the Cat class, so: singleton class of Cat
   end
  end
end

The interesting thing is that when you define a class instance method,
it's actually defined in the singleton class of Cat. So, mew is an
instance method of #<Class:Cat>

2.0.0p195 :010 > class << Cat; p instance_methods.grep(/mew/); end
[:mew]

Jesus.

路路路

On Wed, Jun 18, 2014 at 4:41 PM, Toshi Umehara <toshi@niceume.com> wrote:

Hello,

This is my first post at ruby-talk
I'm a some year exprience of rubyist without deep understanding :frowning:
Now I'm trying to understand more about meta programming.

I browsed other people's sources and have a question.

What does (class << self; self; end) means ?

I did an experience like this.

#!/usr/bin/ruby
class Cat
  def self.mew
    p self
    p (class << self; self; end)
  end
end
Cat.mew

The output was

Cat
#<Class:Cat>

I think the latter( #<Class:Cat> ) means Class singleton object.
Moreover, from my understanding I've been thinking that the former also
means Class singleton object.

Could you please tell the difference between self and (class << self; self;
end) in the context ?

Thank you , Jes煤s Gabriel y Gal谩n

> class << X
>
> opens the singleton class of object X, whatever that is. X could be a
> normal object or a class or whatever.

This explains all !

I read the book, "Metaprogramming Ruby" again and I could understand your words.

Thanks.

路路路

On 06/19/2014 12:41 AM, Jes煤s Gabriel y Gal谩n wrote:

On Wed, Jun 18, 2014 at 4:41 PM, Toshi Umehara <toshi@niceume.com> wrote:

Hello,

This is my first post at ruby-talk
I'm a some year exprience of rubyist without deep understanding :frowning:
Now I'm trying to understand more about meta programming.

I browsed other people's sources and have a question.

What does (class << self; self; end) means ?

I did an experience like this.

#!/usr/bin/ruby
class Cat
   def self.mew
     p self
     p (class << self; self; end)
   end
end
Cat.mew

The output was

Cat
#<Class:Cat>

I think the latter( #<Class:Cat> ) means Class singleton object.
Moreover, from my understanding I've been thinking that the former also
means Class singleton object.

Could you please tell the difference between self and (class << self; self;
end) in the context ?

class << X

opens the singleton class of object X, whatever that is. X could be a
normal object or a class or whatever. Inside that scope, "self" means
the object that is the singleton class of X.

So,

class Cat
   def self.mew
     class << self

opens the scope of the singleton class of the object that happens to
be self at that point. As you are in a class instance method, self is
the object Cat (the class object, remember that classes are objects
too). So,

class Cat
   def self.mew # this creates a class instance method
     p self # here, self is the Cat class object (classes are objects too)
     class << self
       self # this self is the singleton class of the previous line
self, which is the Cat class, so: singleton class of Cat
    end
   end
end

The interesting thing is that when you define a class instance method,
it's actually defined in the singleton class of Cat. So, mew is an
instance method of #<Class:Cat>

2.0.0p195 :010 > class << Cat; p instance_methods.grep(/mew/); end
[:mew]

Jesus.