How to make the class constructor private in Ruby?

Leonid Shevtsov picture Leonid Shevtsov · Oct 14, 2009 · Viewed 18.6k times · Source
class A
private
  def initialize
    puts "wtf?"
  end
end

A.new #still works and calls initialize

and

class A
private
  def self.new
    super.new
  end
end

doesn't work altogether

So what's the correct way? I want to make new private and call it via a factory method.

Answer

adurity picture adurity · Oct 14, 2009

Try this:

class A
  private_class_method :new
end

More on APIDock