Main Content
Where does Ruby load modules from, and how to load from current directory Archive - Originally posted on "The Horse's Mouth" - 2015-06-03 09:42:02 - Graham Ellis
All (scripting) languages allow you to load code from other supporting source files, using keywords like use , load , source and include . In Ruby, the most common way to load other code is to require it - and if you use the require method you'll load in code from another file which will be assumed to have a .rb extension.
But where is the extra code loaded from ? And - more importantly - where do you want it loaded from?
Ruby looks for code it's asked to load in the directories listed in the $LOAD_PATH special variable (also known as $: ) ... taking each of the directories in turn. There's a whole discussion on this variable and manipulating it on StackOverflow - [here] .
Let's take a look at where the codes loaded from on my current system:
WomanWithCat:kingston grahamellis$ irb
irb(main):001:0> require "pp"
=> true
irb(main):002:0> pp $:
["/Library/Ruby/Site/2.0.0",
"/Library/Ruby/Site/2.0.0/x86_64-darwin13",
"/Library/Ruby/Site/2.0.0/universal-darwin13",
"/Library/Ruby/Site",
"/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/vendor_ruby/2.0.0",
"/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/vendor_ruby/2.0.0/x86_64-darwin13",
"/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/vendor_ruby/2.0.0/universal-darwin13",
"/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/vendor_ruby",
"/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0",
"/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/x86_64-darwin13",
"/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin13"]
Any you'll note that Ruby looks first in various local (site) directories, and then into a series of directories that are supplier specific, and finally into general directories. But it does NOT include the current directory, which can be an issue when you're developing and testing code.
You could add a check of your current directory onto the top of your program:
$:.unshift File.dirname(__FILE__)
or (for something really quick and dirty) simply be specific in the require - here's a 'complete' program:
require "./transport"
p (Train.new 9,40,"09:45").getCapacity
Complete source of that (and sample output!) [here] . The cluster of classes that uses [here] .
Some other articles
R108 - More Classes and Objects Testing your new class - first steps with cucumber Build up classes into applications sharing data types in Ruby This article Changing what operators do on objects - a comparison across different programming languages Standard methods available on all objects in Ruby Private, Protected, Public in Ruby. What about interfaces and abstract classes in Ruby? Why you should use objects even for short data manipulation programs in Ruby Ruby - a training example that puts many language elements together to demonstrate the whole Ruby training - some fresh examples for string handling applications Changing a class later on - Ruby Private and Public - and things between Ruby - examples of regular expressions, inheritance and polymorphism What is a factory method and why use one? - Example in Ruby The Multiple Inheritance Conundrum, interfaces and mixins Object Oriented Ruby - new examples Direct access to object variable (attributes) in Ruby Defining a static method - Java, Python and Ruby Tips for writing a test program (Ruby / Python / Java) Ruby objects - a primer Ruby - is_a? v instance_of? - what is the difference? Object Orientation in Ruby - intermediate examples Some Ruby programming examples from our course What are factory and singleton classes? Think about your design even if you don't use full UML MTBF of coffee machines R103 - Basic Ruby Language Elements This article Ruby - the second rung of learning the language Learning to program - variables and constants BODMAS - the order a computer evaluates arithmetic expressions Ruby - standard operators are overloaded. Perl - they are not Sigils - the characters on the start of variable names in Perl, Ruby and Fortran Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. Comparing floating point numbers - a word of caution and a solution Constants in Ruby Variable scope - what is it, and how does it Ruby? Learning to program in Ruby - examples of the programming basics puts - opposite of chomp in Ruby R104 - Control Structures Alternating valuses / flip-flop / toggle - example in Ruby This article Separating your code for easier testing, understanding and re-use; example in Ruby Finding sum, minimum, maximum and average in Python (and Ruby) Conditionals, loops and methods in Ruby - a primer with simple examples Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Muttable v immutable and implications - Ruby Finding the total, average, minimum and maximum in a program Ruby v Perl - a comparison example Assigning values to variables within other statements - Ruby Does a for loop evaluate its end condition once, or on every iteration? Multiple inputs, multiple out, ruby functions Is this number between? Does this list include? - Ruby How a for loop works Java, Perl and other languages Returning multiple values from a function call in various languages - a comparison Ruby training - some fresh examples for string handling applications Splitting data reading code from data processing code - Ruby Why do I need brackets in Ruby ... or Perl, Python, C or Java Alternative loops and conditionals in Ruby and Perl For loop - checked once, or evety time? Ruby v Perl comparison and contrast Passing code to procedures and yield in Ruby A short form of if ... then ... else Learning to program in Ruby - examples of the programming basics Ruby, Perl, Linux, MySQL - some training notes Ruby to access web services Ruby Programming Course - Saturday and Sunday What to do with a huge crop of apples Clean code, jump free (Example in Lua) Saying NOT in Perl, PHP, Python, Lua ... Some Ruby programming examples from our course Ruby, C, Java and more - getting out of loops for loop - how it works (Perl, PHP, Java, C, etc) A better alternative to cutting and pasting code Ruby's case - no break Equality in Ruby - == eql? and equal? Breaking a loop - Ruby and other languages 1st, 2nd, 3rd revisited in Ruby