From: Kevin Mahler Date: 2011-04-15T22:44:04+09:00 Subject: Re: looking for an "inversion" pattern Fearless Fool wrote in post #992947: >> ... >> def define_foo(obj, clazz, method) >> obj.singleton_class.class_eval do >> define_method :foo do >> Class.new do >> define_method method do |*args| >> clazz.send(method, obj, *args) >> end >> end.new >> end >> end >> end > >That's some mighty fine code-fu... Um, no it's not. Inspecting from top to bottom: (a) open the singleton class, (b) define a method, (c) create an instance of a new class (d) define a method. This has nothing to do with code-fu. One of the problems with Ruby is that the syntax changes depending on the compile-time or run-time context, which is a totally arbitrary distinction. It's a disservice to programmers because it makes them say "code-fu" when presented with the run-time counterparts of familiar, mundane compile-time constructs. Don't get me started about using eval to "fix" that. >Since you must call define_foo() for each object & method you want to >cover, I don't see a lot of advantage to the approach. You can automate it as much as you like, including removing the need for define_foo(). Perhaps you took my code too literally. -- Posted via http://www.ruby-forum.com/.