tags:

views:

173

answers:

2

In ruby I frequently use File.expand_path(File.dirname(__FILE__)) for loading config files or files with test data. Right now I'm trying to load some html files for a test in my clojure app and I can't figure out how to do it without hard coding the full path to the file.

edit: I'm using leinigen if that helps in any way

ref: __FILE__ is a special literal which returns the filename (including any path) given to the program when executed. see (rubydoc & perldata)

+9  A: 
*file*

API Reference (add *file* to the url)

Martin Broadhurst
how do you get the full path?
jshen
+7  A: 

Here is one way to replicate that in Clojure:

(defn dirname [path]
  (.getParent (java.io.File. path)))

(defn expand-path [path]
  (.getCanonicalPath (java.io.File. path)))

Then your Ruby line File.expand_path(File.dirname(__FILE__)) in Clojure would be this:

(expand-path (dirname *file*))

See Java interop docs for .getParent & .getCanonicalPath.


NB. I think *file* always returns the absolute (though not canonical) pathname/filename in Clojure. Whereas __FILE__ returns the the pathname/filename provided at execution. However I don't think these difference should effect what your trying to do?

/I3az/

draegtun