Rails class << self Rails class << self ruby ruby

Rails class << self


this

module Utility  class Options #:nodoc:    class << self      # we are inside Options's singleton class      def parse(args)      end    end  endend

is equivalent to:

module Utility  class Options #:nodoc:    def Options.parse(args)    end  endend

A couple examples to help you understand :

class A  HELLO = 'world'  def self.foo    puts "class method A::foo, HELLO #{HELLO}"  end  def A.bar    puts "class method A::bar, HELLO #{HELLO}"  end  class << self    HELLO = 'universe'    def zim      puts "class method A::zim, HELLO #{HELLO}"    end  endendA.fooA.barA.zimputs "A::HELLO #{A::HELLO}"# Output# class method A::foo, HELLO world# class method A::bar, HELLO world# class method A::zim, HELLO universe# A::HELLO world


This is an eigenclass. This question's been asked before.