Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class A
- def initialize(options = {})
- #what goes on in here doesn't really matter for the purpose of this question
- #I just don't want it to be initialized directly
- options.each do |k,v|
- instance_variable_set("@#{k}",v) unless v.nil?
- end
- end
- end
- class B < A
- attr_accessor :class_specific_opt
- def initialize( options = {} )
- @class_specific_opt = "val" unless options[:class_specific_opt].nil?
- super(options)
- end
- end
- class A
- def self.initialize_allowed?
- raise "You cannot initialize instances of this class."
- end
- def self.allow_initialize
- class << self
- def initialize_allowed?
- true
- end
- end
- end
- def initialize(options = {})
- self.class.initialize_allowed?
- options.each do |k,v|
- instance_variable_set("@#{k}", v)
- end
- end
- end
- class B < A
- allow_initialize
- end
- B.new #=> #<B:0x00000102837d10>
- class <<A
- undef :new
- end
- A.new #=> NoMethodError: undefined method `new' for A:Class
- private_class_method :new
- public_class_method :new
- class A
- private_class_method :new
- end
- class B < A
- public_class_method :new
- end
Add Comment
Please, Sign In to add comment