From: Brian Fundakowski Feldman Date: 2001-08-25T00:07:37+09:00 Subject: [ruby-talk:20291] new toy: method_var After seeing a million examples of overriding methods in classes by aliasing an existing method to something with a more obscure name, defining over that method and then calling the alias of the old method in the new method's code, I convinced myself there HAD to be a better way. In that vein, I've implemented the ability to "attach" an anonymous variable to a given method. It works like this: class Foo def initialize(*args) @bob = args end attr_reader :bob end class Foo old = instance_method(:initialize) # Hmm, I feel like overriding # someone's Foo class and adding a # block initializer. def initialize(*args, &block) method_var.bind(self).call(*args) # Call the old constructor @fred = args.collect(&block) if block end define_method_var(:initialize, old) # Attach a copy of the old method to # the current method attr_reader :fred end p Foo.new(:a, :b, :c) {|v| v.to_s} # Pretty simple and generalized, eh? :) You could essentially attach any variable you would like to a method in this fashion, but it's most useful for keeping an "anonymous" copy of the old method around. The implementation I came up with can be found at Let me know what you all think. -- Brian Fundakowski Feldman \ FreeBSD: The Power to Serve! / green@FreeBSD.org `------------------------------'