Skip to content

Instantly share code, notes, and snippets.

@fronx
Created January 4, 2010 16:00
Show Gist options
  • Select an option

  • Save fronx/268611 to your computer and use it in GitHub Desktop.

Select an option

Save fronx/268611 to your computer and use it in GitHub Desktop.

Revisions

  1. fronx renamed this gist Jan 4, 2010. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. fronx revised this gist Jan 4, 2010. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions bury.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Reincarnation for classes

    class Class
    def reincarnate
    buried = Object.__send__(:remove_const, self.name)
    @@ -22,6 +24,8 @@ def foo
    Abc.new.foo


    # Reincarnation for modules

    class Module
    def reincarnate
    buried = Object.__send__(:remove_const, self.name)
  3. fronx revised this gist Jan 4, 2010. 1 changed file with 28 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions bury.rb
    Original file line number Diff line number Diff line change
    @@ -20,3 +20,31 @@ def foo
    end

    Abc.new.foo


    class Module
    def reincarnate
    buried = Object.__send__(:remove_const, self.name)
    Object.const_set(self.name, Module.new).__send__(:include, buried)
    end
    end

    module Boo
    def bar
    "bar"
    end
    end

    Boo.reincarnate

    module Boo
    def bar
    puts super
    end
    end

    class BooTest
    include Boo
    end

    BooTest.new.bar
  4. fronx revised this gist Jan 4, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bury.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    class Class
    def reincarnate
    buried = Object.__send__(:remove_const, self.name)
    Object.const_set(:Abc, Class.new(buried))
    Object.const_set(self.name, Class.new(buried))
    end
    end

  5. fronx revised this gist Jan 4, 2010. 1 changed file with 7 additions and 30 deletions.
    37 changes: 7 additions & 30 deletions bury.rb
    Original file line number Diff line number Diff line change
    @@ -1,45 +1,22 @@
    class Object
    def bury(class_or_module_name)
    Object.instance_eval { remove_const class_or_module_name.to_sym }
    class Class
    def reincarnate
    buried = Object.__send__(:remove_const, self.name)
    Object.const_set(:Abc, Class.new(buried))
    end
    end

    # Example for class self inheritance

    class Abc
    def foo
    "foo"
    end
    end

    class Abc < bury(:Abc)
    Abc.reincarnate

    class Abc
    def foo
    puts super
    end
    end

    Abc.new.foo


    # Example for module self inheritance

    module Xyz
    def bar
    "bar"
    end
    end

    BuriedXyz = bury(:Xyz)

    module Xyz
    include BuriedXyz
    def bar
    puts super
    end
    end

    class XyzTest
    include Xyz
    end

    XyzTest.new.bar
  6. fronx revised this gist Jan 4, 2010. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions bury.rb
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ def bury(class_or_module_name)
    end
    end

    # Example for class "self inheritance"
    # Example for class self inheritance

    class Abc
    def foo
    @@ -21,7 +21,7 @@ def foo
    Abc.new.foo


    # Example for module "self inheritance"
    # Example for module self inheritance

    module Xyz
    def bar
  7. fronx created this gist Jan 4, 2010.
    45 changes: 45 additions & 0 deletions bury.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    class Object
    def bury(class_or_module_name)
    Object.instance_eval { remove_const class_or_module_name.to_sym }
    end
    end

    # Example for class "self inheritance"

    class Abc
    def foo
    "foo"
    end
    end

    class Abc < bury(:Abc)
    def foo
    puts super
    end
    end

    Abc.new.foo


    # Example for module "self inheritance"

    module Xyz
    def bar
    "bar"
    end
    end

    BuriedXyz = bury(:Xyz)

    module Xyz
    include BuriedXyz
    def bar
    puts super
    end
    end

    class XyzTest
    include Xyz
    end

    XyzTest.new.bar