Ruby - Adding a Directory to $Load_Path - What Does It Do

Adding a directory to $LOAD_PATH (Ruby)

I would say go with $:.unshift File.dirname(__FILE__) over the other one, simply because I've seen much more usage of it in code than the $LOAD_PATH one, and it's shorter too!

How to set Ruby's load path externally

See the "Ruby and Its World" chapter from The Pickaxe Book, specifically the section on environment variables. Excerpt:

RUBYLIB 
Additional search path for Ruby programs ($SAFE must be 0).
DLN_LIBRARY_PATH
Search path for dynamically loaded modules.
RUBYLIB_PREFIX
(Windows only) Mangle the RUBYLIB search path by adding this
prefix to each component.

Ruby: how to require a file from the current working dir?

In ruby 1.9.x, you can use the method require_relative. See http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-require_relative.

How is the default Ruby LOAD_PATH determined?

On my machine, the initial load path looks like this:

$ ruby -e 'puts $LOAD_PATH'
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.8.0
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/vendor_ruby/1.9.1
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.8.0
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/vendor_ruby
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/x86_64-darwin10.8.0

Armed with grep, an investigation into the Ruby source leads to the definition of ruby_initial_load_paths[] in version.c (this is on Ruby 1.9.3). The first of these that apply (neither NO_INITIAL_LOAD_PATH or RUBY_SEARCH_PATH have been set) is RUBY_SITE_LIB2. Looking at the defines above that definition we see:

#define RUBY_SITE_LIB2              RUBY_SITE_LIB    "/"RUBY_LIB_VERSION

and in turn:

#define RUBY_SITE_LIB RUBY_LIB_PREFIX"/site_ruby"

Following this chain of defines, it becomes clear that this corresponds to the first entry in my load path above. Similarly the other constants that go into this variable correspond to the other load path entries.

The ruby_initial_load_paths[] variable is used in ruby_init_loadpath_safe() in ruby.c, where the actual load path is set up for the process.

So the answer to your question is that the initial load path is set at compile time with some #defines, according to how the build has been configured.

How to configure RSpec to find specs in lib folder?

RSpec doesn't look for tests in autoload_paths. If you run rspec or rake spec without arguments, RSpec looks in the spec directory by default. If you want to run specs in a different directory, give that directory to rspec as an argument:

rspec app/config/lib/spec

However, it's the universal convention to keep your specs in the spec directory, separate from your code in app and lib, so I'd move your specs to spec/lib. RSpec will then find them without any extra effort.

On Ruby on Rails, how to run ONE functional test when another one is breaking?

Wise-Ass Answer
If you coworker breaks the tests, then he should fix the test, or he shouldn't have committed the code to the repo. That is the main principle that we usually have in projects I work on.

Nice Answer
Try this

rake test:functionals TEST=test/functional/xy_test.rb

Or this

running one test without rake but with explicit $-loadpath works too
here "ruby -I directory" specifies the $loadpath. Otherwise you won't load test environment and "require test_helper" fails!

ruby -I test test/functional/xy_test.rb


Related Topics



Leave a reply



Submit