views:

597

answers:

3
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.

+8  A: 

Try this:

class A
  private_class_method :new
end

More on APIDock

adurity
In case you are looking to implement a Singleton class (the only reason I can think of wanting a private constructor), Ruby will do it for you. http://apidock.com/ruby/Singleton
adurity
And even then someone could do A.send(:new). (BTW, shouldn't "class" be lower case?)
Andrew Grimm
Yes it should. Corrected now.
adurity
A: 

No idea why you would wan to do that but

A.send(:private, :new) should do the trick

Marcin Raczkowski
A: 

(I removed my post, since it wasn't correct)

Dan Zajic
outis
Ok. I get it, I think. Is there a way to delete my "answer"?
Dan Zajic