Sha256: b32b6e527f88aba8f6b7989c4208fb540aa3f5830cd6633c35b71051db05c3c3
Contents?: true
Size: 930 Bytes
Versions: 5
Compression:
Stored size: 930 Bytes
Contents
require "ProtectedConstructor/version" # @example # require 'ProtectedConstructor' # # class Klass # include ProtectedConstructor # # def initialize # end # end # # module KlassFactory # class << self # public # def create # Klass.send(:new) # end # end # end # # # Constructor is not protected. # klass = Klass.new # NoMethodError # # # Example using factory... # klass = KlassFactory::create # works # klass.nil? # false # klass.is_a?(Klass) # true # # # Example just using #send... # klass = Klass.send(:new) # works # klass.nil? # false # klass.is_a?(Klass) # true # module ProtectedConstructor def self.included(klass) klass.module_eval do class << self protected :new def inherited(klass) klass.module_eval do def self.new(*args); super; end end end end end end end
Version data entries
5 entries across 5 versions & 1 rubygems