From: Brian Candler Date: 2008-11-03T22:23:05+09:00 Subject: Re: RubyMacros 0.1.0 Released Caleb Clausen wrote: > A recently requested new feature on ruby-core was __DIR__, which acts > like __FILE__, but returns the directory containing the current source > file. As a macro, that is: > > macro __DIR__ > :( File.dirname __FILE__ ) > end > > Now maybe (now that I've seen your block-as-binding trick) you can > actually write this as a method, something like, > > def __DIR__(&ctx) > File.dirname(eval("__FILE__",ctx)) > end > > I have no ruby ATM, and can't check if that works or not, sorry. It seems to, as long you provide a dummy block: __DIR__{}. I think there's a simpler solution though: def __DIR__ # filename:nnn[:in `method'] File.dirname(caller.first.split(':').first) end Actually, it's a pain that 'caller' doesn't provide a proper composite object with filename, line number, method name. Windows people may have C:... at the front of their filename. A regexp split should fix that. -- Posted via http://www.ruby-forum.com/.