Hello,

> module Bar
>     def self.append_features(cls)
> 
>         class << cls                                     # Trap the
> initialize
>             proc { |x| @init = x }                       # method in an
> unbound
>         end.call(cls.instance_method(:initialize))       # method object
> 
>         cls.class_eval do remove_method :initialize end  # Remove the old
>                                                          # initialize
> 
>         super    # Make sure to call super
>     end
> 
>     def initialize
>         unbound = class << self.class; @init; end  # Retrieve the old method
> 
>         init = unbound.bind self                   # Bind it to the current
>                                                    # object
>         init.call
>         puts "Bar!"    # new stuff goes here
>     end
> end

Somehow I have understand your solution. Anyway, I have some questions
which I cannot figure out with my knowledge from Pragmatic Programmer's
guide.

I do not understand the relation between the body of a class definition,
and the singleton class body.

Let's take this snippet:

class A
  @x=1
  def A.fn1
    puts @x
  end
  def fn2
    puts @x
  end
end

class << A
  @x=2
end

a=A.new
a.instance_eval "@x=3"
class << a
  @x=4
end

A.fn1 # 1
class << A
	puts @x # 2
end
a.fn2 # 3
class << a
  puts @x # 4
end

This prints out 1,2,3,4. For values for 4 @x'es (two for the object and
two for the class). And I didn't used class variables (@@x). If I get
just one object (e.g. the "A" object of type Class), where do these two
instance variables belong? Also, what other techniques are available to
get/set these variables? Maybe there are more @x'es? :)

Thank you:

Circum, who are getting confused in these classes and metaclasses and
meta-metaclasses... :)