Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Load path, load and require in Ruby, and a change from 1.8 to 1.9

Ruby loads files through the require command from a list of directories held in the $: special variable, also known as $LOAD_PATH.

Up to and including Ruby 1.8, this path list included . (the current directory) but from Ruby 1.9 it does not.

Here's a sample program:
  require "direct"
  p Train.new "18:55",96,345


which runs correctly at 1.8.7

  munchkin:rpj12 grahamellis$ /usr/bin/ruby tlo
  ./direct.rb loaded and initialised
  #<Train:0x102c3a840 @seats=345, @length=96, @mycount=1, @time="18:55">


  munchkin:rpj12 grahamellis$ /usr/bin/ruby -v
  ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]


However, I get an error at 1.9.3

  munchkin:rpj12 grahamellis$ ruby tlo
  /Users/grahamellis/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- direct (LoadError)
    from /Users/grahamellis/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from tlo:8:in `<main>'


As a quick (and very dirty) fix, you can add . to the path:
  $:.push(".")
  require "direct"
  p Train.new "18:55",96,345


And the program will work at 1.8 and 1.9, even with the required file in the current running directory.

As an alternative, the require can be modified:
  require "#{File.dirname(__FILE__)}/direct"
  p Train.new "18:55",96,345


There is also a load command in Ruby.. Differences:
require will only bring in a file once, load will bring it in multiple times
require will automatically add extensions .rb, .so, .o and .dll; with load you must be specific
require uses the $: / $LOAD_PATH, but load does not.

And so
  load "direct.rb"
  p Train.new "18:55",96,345

works in both 1.8.7 and 1.9.3. But having said that, require is recommended in most circumstances.
(written 2012-06-24, updated 2012-06-30)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R118 - Ruby Sources and Resources
  [3622] Loading Ruby classes - where does Ruby look? - (2012-02-24)
  [3435] Sorta sorting a hash, and what if an exception is NOT thrown - Ruby - (2011-09-12)
  [3426] Automed web site testing scripted in Ruby using watir-webdriver - (2011-09-09)
  [2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
  [2621] Ruby collections and strings - some new examples - (2010-02-03)
  [2610] Cheat Sheet - what do you need for Ruby on Rails? - (2010-01-31)
  [550] 2006 - Making business a pleasure - (2006-01-01)

R119 - Ruby Miscellany
  [3799] Ruby Documentation through rdoc - (2012-07-07)
  [3428] How many days to Christmas? - (2011-09-09)
  [3155] Rake - a build system using code written in Ruby - (2011-02-03)
  [1890] MySQL database from Ruby - an example - (2008-11-16)
  [1889] Ruby mixins, modules, require and include - (2008-11-16)
  [1720] Some Ruby lesser used functions - (2008-07-26)
  [1586] Variable types in Ruby - (2008-03-21)
  [1181] Good Programming practise - where to initialise variables - (2007-05-09)


Back to
Standard methods available on all objects in Ruby
Previous and next
or
Horse's mouth home
Forward to
Steam train calls at Melksham - Pictures
Some other Articles
Melksham Pride - the Chamber of Commerce, and the future
Improving Wiltshire Rail Offer - it WILL be happening
Programming languages - what are the differences between them?
Steam train calls at Melksham - Pictures
Load path, load and require in Ruby, and a change from 1.8 to 1.9
Standard methods available on all objects in Ruby
Private, Protected, Public in Ruby. What about interfaces and abstract classes in Ruby?
Ruby of Rails - cleanly displaying model data in the view
Adding validation to form entries and sticky fields - Ruby on Rails
Providing a form to allow the user to add data to the model - Ruby on Rails
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3783_Loa ... o-1-9.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat