ruby - How to know if an instance has been initialized? -


how can do:

instance = object.allocate  ## doesn't call initialize(). object sample class. ... # somewhere instance initialized. magic. ... instance.initialized?  ## there's no method that. 

?

of course ask not wanting use flags anywhere. want know general solution default behaviours work simple instance of object.

so ended solution. i'm still open suggestions.

class ::object   def object(name, klass = object, &block)     instance = klass.allocate     if instance.method(:initialize).owner == basicobject       instance.instance_exec(&block) if block_given?     else       raise "no block given." unless block_given?       instance.instance_exec         def initialize(*args)           @__initialized = true           super         end       end       instance.instance_exec(&block)       instance.instance_exec         raise "instance of #{klass} needs initialized." unless @__initialized         remove_instance_variable :@__initialized       end     end     (self.is_a?(module) ? self : self.class).const_set(name, instance)   end end 

i use like

object :hey, someclasswithinitialize   puts "perhaps i'd doing things first before initializing."    initialize    puts "and other things after initializing."    def hey!     puts "hey!"   end end  hey.hey! 

update: turns out there no function in ruby declares flag initialize called. calls rb_obj_call_init rb_obj_dummy nothing significant this.

̶i̶ ̶m̶a̶y̶ ̶j̶u̶s̶t̶ ̶c̶o̶n̶s̶i̶d̶e̶r̶ ̶u̶s̶i̶n̶g̶ ̶s̶i̶m̶p̶l̶e̶ ̶s̶i̶n̶g̶l̶e̶t̶o̶n̶s̶ ̶f̶o̶r̶ ̶i̶n̶s̶t̶a̶n̶c̶e̶s̶ ̶t̶h̶a̶t̶ ̶d̶o̶e̶s̶n̶'̶t̶ ̶i̶n̶h̶e̶r̶i̶t̶ ̶a̶n̶y̶t̶h̶i̶n̶g̶,̶ ̶a̶n̶d̶ ̶̶s̶i̶n̶g̶l̶e̶t̶o̶n̶#̶i̶n̶s̶t̶a̶n̶c̶e̶̶ ̶f̶o̶r̶ ̶a̶n̶y̶t̶h̶i̶n̶g̶ ̶t̶h̶a̶t̶ ̶d̶o̶e̶s̶ ̶o̶v̶e̶r̶ ̶t̶h̶i̶s̶ ̶m̶e̶t̶h̶o̶d̶ ̶b̶u̶t̶ ̶i̶t̶ ̶w̶o̶u̶l̶d̶ ̶o̶n̶l̶y̶ ̶b̶e̶ ̶c̶o̶n̶c̶l̶u̶d̶e̶d̶ ̶w̶h̶e̶n̶ ̶i̶ ̶a̶c̶t̶u̶a̶l̶l̶y̶ ̶u̶s̶e̶ ̶o̶n̶e̶.̶ classes declared singleton not inherit. forgot.

using alias , method not needed.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -