# Making constructor private in Ruby - a la Singleton pattern

**URL:** <https://www.ruby-forum.com/t/making-constructor-private-in-ruby-a-la-singleton-pattern/66455>\
**Category:** Ruby\
**Created:** [August 25, 2006, 6:22pm UTC](https://www.ruby-forum.com/t/making-constructor-private-in-ruby-a-la-singleton-pattern/66455 "2006-08-25T18:22:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [August 25, 2006, 6:22pm UTC](https://www.ruby-forum.com/t/making-constructor-private-in-ruby-a-la-singleton-pattern/66455/1 "2006-08-25T18:22:41Z")

</div>

All,

I’ve been doing singletons in Java for years, but I’m having trouble  
getting a similar scheme to work in Ruby.

I want to make the create() method of an ActiveRecord object private so  
that it can’t be invoked by a client and the client will be forced to  
use a class method to construct the object.

Here’s the method that I want to be the public class method to construct  
the object:

def self.create\_as\_artisan()  
quote\_input = QuoteInput.create(:user\_id =\> “1”, :program\_id =\> “1”)  
artisanqi = self.create()  
artisanqi.quote\_input = quote\_input  
end

I’m having trouble understanding how to make the constructor of the  
object private.

I want to make it so that create\_as\_artisan has access to self.create()  
but no one else does.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [August 25, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/making-constructor-private-in-ruby-a-la-singleton-pattern/66455/2 "2006-08-25T18:26:39Z")

</div>

> Here’s the method that I want to be the public class method to construct  
> the object:
> 
> def self.create\_as\_artisan()  
> quote\_input = QuoteInput.create(:user\_id =\> “1”, :program\_id =\> “1”)  
> artisanqi = self.create()  
> artisanqi.quote\_input = quote\_input  
> end

I try this (these are class methods):

public  
def self.create()  
raise “Exception”  
end

private  
def self.create()  
super  
end

and the object is created! Why isn’t the private modifier working.

Wes
