Skip to content

Instantly share code, notes, and snippets.

@MrJoy
Created April 17, 2014 20:54
Show Gist options
  • Save MrJoy/11010959 to your computer and use it in GitHub Desktop.
Save MrJoy/11010959 to your computer and use it in GitHub Desktop.
Make the constructor private in Ruby.
class Example
class << self
def my_alternative_constructor
# From here you can call .new, and do fun things like say, call .freeze before returning the instance.
return Example.new.freeze
end
protected
alias_method :private_new, :new
def new(*args)
private_new(*args)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment