Ruby class << abcd syntax

I know that there were other syntax questions class << self. However, I did not find these answers clear enough. I have experience in Java / C #, C, so I don't like Ruby. I read that class << selfbelongs to the singleton class. I find this curious complex, so I would like to understand what the operator does <<in this context and what can be put on both ends. I tried to write simple code to help me understand this syntax (my questions are in code):

class Self

def Self.selfTest
end

def onSelf
    class << Self   #I know this might be strange. 
        self
    end

end

def onself
    class << self
        self
    end
end

end

s = Self.new
onSelf = s.onSelf
onself = s.onself

#Here, i wanna know what kind of references are returned. 

puts "onSelf responds to onSelf:#{onSelf.respond_to?(:onSelf)}"
puts "onSelf responds to selfTest:#{onSelf.respond_to?(:selfTest)}"
puts "onself responds to onSelf:#{onself.respond_to?(:onSelf)}"
puts "onself responds to selfTest:#{onself.respond_to?(:selfTest)}"

#Output:
#onSelf responds to onSelf:false
#onSelf responds to selfTest:false
#onself responds to onSelf:false
#onself responds to selfTest:true

#So, i conclude that the second one is a reference to a class. What is the first one???????


puts onSelf
puts onself

#Output
#<Class:Self>
#<Class:#<Self:0x007f93640509e8>>

#What does this outputs mean???????

def onSelf.SelfMet
    puts 'This is a method defined on base class'
end


def onself.selfMet
    puts 'This is a method defined on metaclass'
end



puts "Does Self Class respond to SelfMet? : #{Self.respond_to?(:SelfMet)}"
puts "Does Self Class respond to selfMet? : #{Self.respond_to?(:selfMet)}"
puts "Does Self instance respond to SelfMet? : #{s.respond_to?(:SelfMet)}"
puts "Does Self instance respond to selfMet? : #{s.respond_to?(:selfMet)}"

#Output
#Does Self Class respond to SelfMet? : false
#Does Self Class respond to selfMet? : false
#Does Self instance respond to SelfMet? : false
#Does Self instance respond to selfMet? : false

#Why won't they respond to defined methods???? 

thank

: . , . , , Ruby , . , < . , < abcd, abcd singleton. , singleton Singleton. singleton . :

singleton Self- > singleton Object- > Singleton basicobject → class- > module- > object- > kernel- > basicObject

singleton- :

singleton- > Self- > Object- > kernel- > basicObject

.

+3
2

Ruby . : , .

singleton , , , , . , : , .

:

foo, bar, baz = Object.new, Object.new, Object.new

class << foo; def quux; :foo end end
class << bar; def quux; :bar end end

foo.quux # => :foo
bar.quux # => :bar
baz.quux # NoMethodError

- , . , , . , , , , , , , , , , , , , , .

, - Java, : Java , ( ). Ruby (), ( ).

, , <

, .

.

, class, - , .

+4

, . , :

puts onSelf                         #=> #<Class:Self> 
puts Self.singleton_class           #=> #<Class:Self>
puts onSelf == Self.singleton_class #=> true

puts onself                         #=> #<Class:#<Self:0x007fe6330aab10>>
puts s.singleton_class              #=> #<Class:#<Self:0x007fe6330aab10>>
puts onself == s.singleton_class    #=> true

, , , :

puts onSelf.SelfMet
  #=> This is a method defined on Self metaclass

puts onself.selfMet
  #=> This is a method defined on s metaclass

, onSelf (Self.singleton_class) SelfMet ( SelfMet), onSelf (s.singleton_class) SelfMet ( SelfMet).

Self SelfMet def Self.SelfMet... def self.SelfMet.. ( ), .

, s SelfMet, def selfMet... ( Self ) s singleton class: def s.selfMet... ( Self ).

Self , s ; Self s .

, . p self, #=>, , #=>, Ruby. , , class Dog, class << self , Self , . p self - .

p self            #=> main

class Dog
  p self          #=> Dog

  def self.woof1
    p self
  end

  def Dog.woof2
    p self
  end

  p self          #=> Dog

  class << self
    p self                #=> #<Class:Dog>
    p Dog.singleton_class #=> #<Class:Dog>  
    def woof3
      p self
    end
  end

  p self          #=> Dog

  def woof4
    p self
  end
end

p self            #=> main

def Dog.woof5
  p self
end

Dog.instance_eval do
  p self          #=> Dog
  def woof6
    p self
  end
end  

dog = Dog.new     #=> #<Dog:0x007fe17b08cf00> 

def dog.woof7
  p self
end

dog.instance_eval do
  p self          #=> #<Dog:0x007fe17b08cf00>
  def woof8
    p self
  end
end  

p self            #=> main

Dog.woof1 #=> Dog
Dog.woof2 #=> Dog
Dog.woof3 #=> Dog
Dog.woof5 #=> Dog
Dog.woof6 #=> Dog

dog.woof4 #=> #<Dog:0x007fe17b08cf00>
dog.woof7 #=> #<Dog:0x007fe17b08cf00>
dog.woof8 #=> #<Dog:0x007fe17b08cf00>

puppy = Dog.new #=> #<Dog:0x007fe17ba93a08>
puppy.woof4     #=> #<Dog:0x007fe17ba93a08>
puppy.woof7 #=> undefined method `woof7' for #<Dog:0x007fe5fb3e1d48>
puppy.woof8 #=> undefined method `woof8' for #<Dog:0x007fe5fb3e1d48>

, . , . . , , .

, Ruby .

+2
source

All Articles