Nay nay! You cannot combine these two techniques to make a private class method. The private keyword does some modal thing with respect to instance methods being defined in the block, but the syntax def self.method_name is a different kind of thing. That different kind of thing applies to any object:

three = BasicObject.new
def three.to_i
3
end
three.to_i
#=> 3

The def something.method_name semantics ignores any declaration about privacy. Here’s a question: Where are the methods three.to_i and three.to_s defined? In something called a singleton class, also called an eigenclass. These methods are called singleton methods because they apply to three but not to anything else:

When you use the class << x ... end syntax, the code in the block is evaluated in the context of the singleton class of x for any object x. Note that it works just like defining instance methods in a typical class declaration. For example, we can include a module:1

module Mathy
def * that
self.to_i * that.to_i
end
end
class << four
include ::Mathy
end
four * 5
#=> 20

What happens if we create a singleton method for a class object?

class<<Sampledefglitch'Gremlins Lurking In The Computer Hardware'endend

Hey, what’s a class method anyways? It’s a method on the class, not a method on an instance of the class. In other words… Class methods are singleton methods of class objects, and thus you can define them with either def Sample.glitch or class << Sample.

homework

You are thinking that you can .extend any object with a module too. Well, you can extend any instance of Object, but not an instance of BasicObject that isn’t also an instance of Object. Tricky raganwald! ↩