From: Max Muermann Date: 2007-01-31T14:59:14+09:00 Subject: Re: Tricks with method as superclass > Cool, that worked well. Now I want to keep the args and block passed > that was passed to the method, so I tried the following, which makes > an anonymous subclass of Foo, and Bar is a subclass of that: > > class Foo > def initialize(*args) > puts "Initializing Foo" > end > end > => nil > > def Foo(*args, &block) > puts args.inspect > block.call if block > Class.new(Foo).instance_eval <<-END > def initialize > # I'm going to do something with block and args here > puts "Double surprise!" > end > self > END This is really cool. Regarding the double surprise problem, class_eval instead of instance_eval (and a call to super) does the trick: Class.new(Foo).class_eval <<-END def initialize super # I'm going to do something with block and args here puts "Double surprise!" end self END => ["Hi there!"] Surprise! Initializing Foo Double surprise! --max