From: James Britt Date: 2007-01-17T00:53:53+09:00 Subject: Re: Q: How can a Rake task know the caller's directory? Jim Weirich wrote: > James Britt wrote: >> I'm increasingly using rake files for common system tasks. Since Rake >> will search up a directory tree until it finds a Rakefile, I can have >> one in my home directory and call tasks from anyplace below that. >> >> But tasks that need to know something about the current directory have >> me stymied. Calling Dir.pwd in a task reflects the directory of the >> Rakefile. >> >> Is there a rake method that returns the path of where the Rakefile >> search began? >> >> >> E.g., if I'm in /home/james/foo/bar, and the nearest Rakefile is in >> /home/james, with task :whereami, and I call >> >> $ rake whereami >> >> what does :whereami have to do to return '/home/james/foo/bar' ? >> >> Thanks! > > task :whereami do > puts Rake.original_dir > end I had looked for such a method in ri and the Rake rdocs, but saw nothing that looked like what I wanted. $ ri Rake ------------------------------------------------------------ Class: Rake (no description...) ------------------------------------------------------------------------ I could not find the originaL_dir method listed in the rdoc either, until I walked through the source code. Because of the way this method is defined it Rake, rdoc will not correctly process it as a class method on Rake itself: # in lib/rake.rb # class << self # Current Rake Application def application @application ||= Rake::Application.new end # Set the current Rake application object. def application=(app) @application = app end # Return the original directory where the Rake application was # started. def original_dir application.original_dir end end And in the rdoc for Rake::Application, the method is not listed in the list of methods because it gets rdoc'ed as an "attribute". One has to already know where to look, and what to look for, to find the behavior. -- James Britt "I never dispute another person's delusions, just their facts." - Len Bullard