1-Testing Automation Tips n Tricks — 01 August 2011
‘Current Directory’ removed from $LOAD_PATH in Ruby 1.9.2

I was practicing sample code in Everyday Scripting with Ruby: For Teams, Testers, and You by Brian Marick and I came across an issue for which it took me a while to find a workaround.

I was testing the ‘churn’ sample from the book. The actual issue was I had one ruby file which contained the code base called ‘churn.rb’ and then there was another file to unit-test the code called ‘churn-tests.rb’. Both these files were under a custom folder under ‘D:\blah\blah\code\’

‘Churn-tests.rb’ includes ‘churn.rb’ file to be able to test it. The command used to include is: “require ‘churn’”.
Initially in previous versions, since both files were in the same folder, Ruby would simply load the code file – ‘churn’. But as of Ruby v1.9.2, as part of the default installation, the code directory: “.” has been removed from the $LOAD_PATH.

As a result my code (unit-test) just wouldn’t work. I tried various options but no luck.

Finally, I came across few ways of fixing it, and those are:

1 – Provide the relative path:

require ‘./churn’

2 – Get current file’s path and reuse it:

require File.expand_path(File.join(File.dirname(__FILE__), ‘filename‘))

3 – Same as above but bit more readable:

require Pathname.new(__FILE__).dirname + ‘filename’

4 – Spell out the complete path, but obviously that’s not ideal

Hope that comes handy to someone.

Happy Coding… oops… Testing!

Related Articles

Share

About Author

(0) Readers Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>