Revision: 50317
Updated Code
at August 18, 2011 04:38 by m1b
Updated Code
require 'logger' $log = Logger.new(STDOUT) $log.level = Logger::INFO $log.progname = "LifecycleDemo" $log.formatter = proc { |severity, datetime, progname, msg| #"#{datetime.strftime("%Y-%b-%d %a %H:%M")} #{severity} #{progname} {#{msg}}\n" "#{msg}\n" } #$log.datetime_format = "%a %Y-%m-%d %H:%M" class FirstClass @@grandClass = "FirstClassVar" # class level variable to track def initialize @grandVar = "FirstInstanceVar" # instance variable to track $log.info("! FirstClass init, #{ids}") end def grand $log.info("> FirstClass grand, #{ids}") end def ids "#{self.class.name}:: #{@@grandClass}:#{@@grandClass.object_id}, #{@grandVar}:#{@grandVar.object_id}" end end class SecondClass < FirstClass def initialize @ego_hilite = "-" * 15 super $log.info("! Second class init, #{ids}") end def spill_it $log.info("> Spill It call, #{ids}") end def alter_ego @grandVar = "SecondInstanceVar" @@grandClass = "SecondClassVar" $log.info("\n#{@ego_hilite} Ego altered by the #{self.class.name} #{@ego_hilite}\n\n") end end class ThirdClass < SecondClass @@grandClass = "ThirdClassStatic" def initialize super $log.info("! 3rd class init, #{ids}") end def alter_ego @grandVar = "ThirdInstanceVar" @@grandClass = "ThirdClassVar" $log.info("\n#{@ego_hilite} Ego altered by the Third Class #{@ego_hilite}\n") end end def test(instance) hilite = "*" * 20 $log.info("\n\n#{hilite} Testing instance of #{instance.class.name} BEGIN #{hilite}") instance.grand instance.spill_it # see what gets changed if anything: instance.alter_ego instance.grand instance.spill_it $log.info("\n#{hilite} Testing instance of #{instance.class.name} END #{hilite}") end test(SecondClass.new) test(ThirdClass.new)
Revision: 50316
Updated Code
at August 18, 2011 03:20 by m1b
Updated Code
class FirstClass @@grandClass = "FirstClassVar" # class level variable to track def initialize @grandVar = "FirstInstanceVar" # instance variable to track puts "! FirstClass init, #{ids}" end def grand puts "> FirstClass grand, #{ids}" end def ids "#{self.class.name}:: #{@@grandClass}:#{@@grandClass.object_id}, #{@grandVar}:#{@grandVar.object_id}" end end class SecondClass < FirstClass def initialize @ego_hilite = "-" * 15 super puts "! Second class init, #{ids}" end def spill_it puts "> Spill It call, #{ids}" end def alter_ego @grandVar = "SecondInstanceVar" @@grandClass = "SecondClassVar" puts "\n#{@ego_hilite} Ego altered by the #{self.class.name} #{@ego_hilite}\n\n" end end class ThirdClass < SecondClass @@grandClass = "ThirdClassStatic" def initialize super puts "! 3rd class init, #{ids}" end def alter_ego @grandVar = "ThirdInstanceVar" @@grandClass = "ThirdClassVar" puts "\n#{@ego_hilite} Ego altered by the Third Class #{@ego_hilite}\n" end end def test(instance) hilite = "*" * 20 puts "\n\n#{hilite} Testing instance of #{instance.class.name} BEGIN #{hilite}" instance.grand instance.spill_it # see what gets changed if anything: instance.alter_ego instance.grand instance.spill_it puts "\n#{hilite} Testing instance of #{instance.class.name} END #{hilite}" end test(SecondClass.new) test(ThirdClass.new)
Revision: 50315
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at August 18, 2011 03:04 by m1b
Initial Code
class FirstClass @@grandClass = "FirstClassVar" # class level variable to track def initialize @grandVar = "FirstInstanceVar" # instance variable to track puts "! FirstClass init, #{ids}" end def grand puts "> FirstClass grand, #{ids}" end def ids "#{@@grandClass}:#{@@grandClass.object_id}, #{@grandVar}:#{@grandVar.object_id}" end end class SecondClass < FirstClass def initialize super puts "! Second class init, #{ids}" end end class ThirdClass < SecondClass def initialize super puts "! 3rd class init, #{ids}" end def spill_it puts "> Spill It method call from 3rd class, #{ids}" end def alter_ego @grandVar = "ThirdInstanceVar" @@grandClass = "ThirdClassVar" puts "\n#{"*" * 15} Ego altered by the Third Class #{"*" * 15}\n\n" end end third = ThirdClass.new third.grand third.spill_it # see what gets changed if anything: third.alter_ego third.grand third.spill_it
Initial URL
Initial Description
Initial Title
Ruby class and instance variables lifecycle across inheritance demo
Initial Tags
ruby
Initial Language
Ruby