Class.new and extending it

I am trying to do something like the following code sample:

class Base
  class << self
    attr :my_name, true
  end

  def show_name
    self.class.my_name
  end
end

class Maker
  def Maker.create_class(class_name)
    Class.new(Base) { self.my_name = class_name }
  end
end

HEH = Maker.create_class("HEH")

heh_instance = HEH.new
p heh_instance.kind_of?(Base)
p heh_instance.show_name

class ExtendedHEH < HEH
end

p ExtendedHEH.new.show_name

  I want the last 'p' to print out "HEH". I understand why Base.show_name
is not working for the extended class (self resolves to itself, which will grab the extended class 'ExtendedHEH')... but I do not understand how I can access
the singleton class to get at "HEH". Any thoughts?

-Tom

···

--
+ http://www.tc.umn.edu/~enebo +---- mailto:enebo@acm.org ----+

Thomas E Enebo, Protagonist | "Luck favors the prepared |
                             > mind." -Louis Pasteur |

class Base
  class << self
    attr :my_name, true

     def inherited(kl)
        kl.my_name = self.my_name unless kl.my_name
        super
     end

  end

Guy Decoux

Thanks! I have a strange variation on this (using 'inherited') that works
but yours is much cleaner :slight_smile:

-Tom

On Mon, 12 Sep 2005, ts defenestrated me:

···

> class Base
> class << self
> attr :my_name, true

     def inherited(kl)
        kl.my_name = self.my_name unless kl.my_name
        super
     end

> end

Guy Decoux

--
+ http://www.tc.umn.edu/~enebo +---- mailto:enebo@acm.org ----+

Thomas E Enebo, Protagonist | "Luck favors the prepared |
                             > mind." -Louis Pasteur |