What Are the Paths That "Require" Looks Up by Default

What are the paths that require looks up by default?

It depends on your platform, and how Ruby was compiled, so there is no "the" answer to this. You can find out by running:

ruby -e 'puts $:'

Generally, though, you have the standard, site, and vendor Ruby library paths, including an arch, version, and general directory under each.

What does $: . do to Ruby's require path?

  1. $: is the variable that holds an array of paths that make up your Ruby's load path
  2. << appends an item to the end of the array
  3. . refers to the current directory

    1   2  3
    | | |
    V V V
    $: << "."

So you are adding the current directory to Ruby's load path

References:

  1. Can be found in the Execution Environment Variables section of of this page from The Pragmatic Programmers Guide

    An array of strings, where each string specifies a directory to be searched for Ruby scripts and binary extensions used by the load and require methods. The initial value is the value of the arguments passed via the -I command-line option, followed by an installation-defined standard library location, followed by the current directory (“.”)[Obviously this link is for an older version of Ruby as this is still in there]. This variable may be set from within a program to alter the default search path; typically, programs use $: << dir to append dir to the path.

  2. Can be found in the docs for array at ruby-doc.org.

    Append—Pushes the given object on to the end of this array. This expression returns the array itself, so several appends may be chained together.

configure: Specify a path to search in for headers AFTER the default paths

Using gcc you might be successful using -idirafter. Keep in mind though that configure may modify your search path with -I on its own, for example when it finds a library in a custom location, it might (and probably will) add -I for this location to all subsequent tests and final compilation, which might still include other packages from this new location if they are available there instead of default location.

GCC how to add before the default linker search path by default? LIBRARY_PATH not working

As the GCC manual says, LIBRARY_PATH is the correct environment variable to add directories to the library search path.

If you add -v to the g++ command you should see the LIBRARY_PATH that it uses, and you should see it includes the directory you have specified, and that it gets added to the collect2 command as -L, but you will see it gets added after the standard directories such as -L/usr/lib etc.

I don't know any way to make the directories in LIBRARY_PATH come first, I think you have to use -L for that.

How to change default path for images to look for

You you mean the image_tag helper is looking by default there? That's only the case if you only specify a relative path. Putting the full path in gets what you want I believe.

<%= image_tag '/data/model/1'

would generate

<img src="/data/model/1.png">

How to change the default path of view files in a Rails 3 controller?

If there's no built-in method for this, perhaps you can override render for that controller?

class MyController < ApplicationController
# actions ..

private

def render(*args)
options = args.extract_options!
options[:template] = "/mycustomfolder/#{params[:action]}"
super(*(args << options))
end
end

Not sure how well this works out in practice, or if it works at all.



Related Topics



Leave a reply



Submit