On Thu, May 04, 2006 at 06:59:59AM +0900, polypus wrote:
> 
> given the below, if you were not allowed to modify class X
> 
> module N
>   def foo
>   end
> end
> 
> module M
>   # ...
> end
> 
> class X
>   extend M
> end
> 
> would it possible for
> 
> x = X.new
> 
> to somehow call foo as an instance method of x?

module N
  class << self; attr_accessor :bleh end # just to prove #foo was run
  self.bleh = "#foo not executed"
  def foo
    N.bleh = "#foo was executed!!"
  end
end

module M
  def self.extend_object(o)
    o.class_eval do
      include N
      def self.new(*a,&b)
        r = super(*a, &b) # explicit -> less stuff to remember
	r.foo # is this what you meant?? weird.
	      # why won't X.new.foo do?
	r
      end
    end
  end
end

# ===== unmodified =>
class X
  extend M
end

x = X.new                                          # => #<X:0xb7e2f034>
# <=====
RUBY_VERSION                                       # => "1.8.4"
N.bleh                                             # => "#foo was executed!!"


Now, care to explain the intended use of this?
Or is it just a pointless exercise?

-- 
Mauricio Fernandez  -   http://eigenclass.org   -  singular Ruby