shlogg · Early preview
Augusts Bautra @augustsbautra

Defining Private Class Methods In Ruby

Defining class methods as private in Ruby requires using `private_class_method` and placing them after the `private` keyword. Rails provides an alternative with `extend ActiveSupport::Concern`.

Unfortunately, defining class methods as private is a bit more involved than simply defining them after private.

  
  
  The incorrect way


module Operations
  private
  def self.private_operation
    "secret"
  end
end
Operations.private_operation #=> "secret" # oh, no!

    
    

    
    




  
  
  The correct way


module Operations 
  private_class_method def self.private_operation
    "secret"
  end
end
Operations.private_operation
#=> private method `private_operation' called for Operations:Module (NoMethodError)

    
    

    
    




It's probably a good practice to place thes...