This summer, I’m learning Ruby on Rails at Metis, a 12-week class taught by some great folks from thoughtbot. This post is part of a series sharing my experience and some of the things I’m learning.

File this one under, “fool me once, shame on you; fool me twice, write a blog post.”

In Ruby we use the private method to define instance methods that are only accessible from within a class:

class HammerTime
  # Arbitrary public methods omitted

  def stop
    puts "Can't touch this"

Calling stop on an instance of the class HammerTime from somewhere other than within the class itself will result in an error:
# => NoMethodError: private method `stop' called for #<HammerTime:0x00>

Given this, we might expect the same syntax to work for class methods:

class I
  # Arbitrary public methods omitted


  def self.wanna
    puts " with somebody"
    puts "...feel the heat with somebody"

Surely we’ll get the same NoMethodError, right? Wrong:

# => with somebody
# => ...feel the heat with somebody

Instead, we’ll need to explicitly define this method as a private_class_method, like this:

class Domo
  # Arbitrary public methods omitted

  def self.arigato
    puts "Secret secret, I've got a secret"
  private_class_method :arigato

And we’ll get the expected error, indicating this method is only accessible from within the class:

# => NoMethodError: private method `arigato' called for Domo:Class

Caveat emptor.