New to Ruby and Am Having Trouble with Load_Path

New to Ruby and am having trouble with LOAD_PATH

Ruby 1.8.7 is shipped with OS X. It seems that gem is seeing that version instead of your manually installed 2.0.0. As others already suggested, using RVM or rbenv (which one depends on personal preference) makes handling different ruby environments much easier. You can find those tools and descriptions for them in the following places:

RVM: https://rvm.io/

rbenv: https://github.com/sstephenson/rbenv

ruby require problem (something to do with $LOAD_PATH)

To see if the two executables are different versions of ruby (as suspected by some), ask it to do

puts RUBY_VERSION

Rubygems. A newly installed gem isn't added to $LOAD_PATH ($:)

You haven't loaded any gems yet:

ruby -e '
puts "Before require: #{$:.grep /rack/}"
require "rack"
puts "After require: #{$:.grep /rack/}"
'
# Before require: []
# After require: ["C:/Ruby/YARV/1.9.2/lib/ruby/gems/1.9.1/gems/rack-1.2.1/bin",
# "C:/Ruby/YARV/1.9.2/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib"]

ruby -e '
puts "Before gem: #{$:.grep /rack/}"
gem "rack"
puts "After gem: #{$:.grep /rack/}"
require "rack"
puts "After require: #{$:.grep /rack/}"
'
# Before gem: []

# After gem: ["C:/Ruby/YARV/1.9.2/lib/ruby/gems/1.9.1/gems/rack-1.2.1/bin",
# "C:/Ruby/YARV/1.9.2/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib"]
# After require: ["C:/Ruby/YARV/1.9.2/lib/ruby/gems/1.9.1/gems/rack-1.2.1/bin",
# "C:/Ruby/YARV/1.9.2/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib"]

Why is the structure of my ruby project like this and why use load path?

As your app currently stands it wouldn't make any difference.

With a real project there would be many more files though and you would require them through lib/codebreaker.rb so that users of your library just need to require the one file.

You wouldn't want to duplicate that list of requires in env.rb, so your specs load lib/codebreaker.rb, just like any other user of your library.

Understanding Ruby's load paths

Ruby's $LOAD_PATH will not include your lib directory by default (even though that's where the file you're running is located).

You can either tell the ruby interpreter to include it:

ruby -Ilib lib/processor.rb

Or you can add the lib folder to the load path:

$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'processor/mapper'
...

Add $LOAD_PATH externally

RUBYLIB environment variable is a colon separated list of paths which ruby will prepend to the standard LOAD_PATH. ruby -I path on the command line is also the same as $LOAD_PATH.unshift 'path' in your code. Ruby will also process options from environment var RUBYOPT.

Adding a directory to the load path in Rails?

In the current version of Rails (3.2.8), this has been changed in the application.rb file.

The code is currently commented out as:

  # Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)

Will need to update the autoload_paths value. Attempting to modify the the former load_paths variable causes this error.

/configuration.rb:85:in `method_missing': undefined method `load_paths' for #<Rails::Application::Configuration:0xac670b4> (NoMethodError)

for an example, for each path to add to autoload_paths config, add a line similar to the following:

config.autoload_paths += %W(#{config.root}/app/validators)

config.autoload_paths accepts an array of paths from which Rails will autoload constants. Default is all directories under app.

http://guides.rubyonrails.org/configuring.html


From commentor (hakunin) below:

If the directory is under app/, you don't need to add it anywhere, it should just work by default (definitely in 3.2.12). Rails has eager_load_paths that acts as autoload_paths in development, and eager load in production. All app/* directories are automatically added there.

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!



Related Topics



Leave a reply



Submit