Ruby’s class << self is a common method for creating class methods, although it becomes very difficult to read in a large class.

class Restaurant
 
  class << self
    def location
      puts "Manhattan"
    end
  end
 
end 
 
The above doesn't have any benefit other saving a few keystrokes.
 
<pre lang="ruby">
class Restaurant
 
  def self.location
    puts "Manhattan"
  end 
 
end

There is an important time to use the class << self syntax. When we are setting getter and setters on the class, not the instance.

class Restaurant
 
  @created_at = nil 
 
  class << self
    attr_accessor :created_at
  end 
 
  def initialize
    # ...
    Restaurant.created_at = Time.now
  end 
 
end
 
Restaurant.new
puts "This Restaurant was created at #{Restaurant.created_at}"
 
sleep(5)
Restaurant.new
puts "This Restaurant was created at #{Restaurant.created_at}"

:)

One thought on “Daily Ruby Tips #115 – A Good Use of class << self

  1. Tip Follower says:

    Good :)

Leave a Reply

Your email address will not be published. Required fields are marked *