Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 454 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
load works on local path, require doesn't

#1
loadee.rb

puts '> This is the second file.'

loaddemo.rb

puts 'This is the first (master) program file.'
load 'loadee.rb'
puts 'And back again to the first file.'

When I run `"ruby loaddemo.rb"`, This works fine. Both files are in the same directory, and that's the directory I run from.

But if I change the load to a require, and with or without the extension I get:

<internal:lib/rubygems/custom_require>:29:in `require': no such file to load
-- loadee.rb (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from loaddemo.rb:2:in `<main>'

My question is of course, why isn't require working in this case? It should, right? Do load and require use different paths?

Ruby version 1.9.2
Reply

#2
If you provide just a filename to `require`, it will only look in the predefined `$LOAD_PATH` directories. However, if you provide a path with your filename, it should work:

puts 'This is the first (master) program file.'
require './loadee.rb'
puts 'And back again to the first file.'


You could also add your project's folder to the load path instead:

$LOAD_PATH.unshift File.dirname(__FILE__)
puts 'This is the first (master) program file.'
require 'loadee.rb'
puts 'And back again to the first file.'

And last, you could just use `require_relative` instead:

puts 'This is the first (master) program file.'
require_relative 'loadee.rb'
puts 'And back again to the first file.'


Reply

#3
Providing a path with the filename seemed not to work for me, and I didn't want to cram a bunch of paths into my `$LOAD_PATH`.

Checking out the [documentation][1], I found `require_relative`.

require_relative 'loadee'

Works in both `1.9.2` and `2.1.2`.

The [documentation][2] indicates that `require` is not intended to search relative paths at all, and neither is `load`.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through