Skip to content

Instantly share code, notes, and snippets.

@sandheepg
Created March 8, 2017 07:55
Show Gist options
  • Save sandheepg/18f3a1e21f35b226c0590ba1ac6fb17f to your computer and use it in GitHub Desktop.
Save sandheepg/18f3a1e21f35b226c0590ba1ac6fb17f to your computer and use it in GitHub Desktop.
$: in ruby

In Rails programming you might seldom come across the below statement

$:.unshift File.dirname(__FILE__)

$: represents load path in Ruby. Its a pre-defined variable and short hand for $LOAD_PATH which ruby uses to look for files.

unshift prepends the given path to the beginning of the array (load path).

__FILE__ gives the relative path of the file from the current execution directory. Incase, you need absolute path, use

File.expand_path(__FILE__)

NOTE : This explanation is as of Ruby 1.8.6. Latest versions of Ruby behave differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment