Why doesn't relative_require work on Ruby 1.8.6?
Edit:
Back in the days where this question was asked it referred to Ruby 1.8.6 where there was no require_relative
. By now Ruby 1.8.6 is outdated and shouldn't be used anymore.
Original:
There is simply no method name require_relative
. You can use require
there aswell.
The require_relative
function is included in an extension project to the Ruby core libraries, found here: http://www.rubyforge.org/projects/extensions
You should be able to install them with gem install extensions
.
Then in your code add the following line before the require_relative
:
require 'extensions/all'
Why relative path doesn't work in Ruby require
Relative path is based on working dir. I assume that there is main file on the same directory. If you run ruby ./shapes/main.rb
on project root, ruby try to find {project_root}/shape.rb
, not {project_root}/shapes/shape.rb
. It doesn't work.
You need to use require_relative
like below.
# {project_root}/shapes/main.rb
require_relative './shape'
require_relative './rectangle'
require_relative './square'
Why doesn't Ruby 'require' allow relative paths?
tl;dr: IRB is special and has some odd rules. Ruby in general works just fine with relative paths.
require
will search the load path (which you can see by inspecting $:
or $LOAD_PATH
). This will not include the directory that you launched IRB from:
> $:
=> ["/usr/local/rvm/rubies/jruby-head/lib/ruby/2.2/site_ruby", "/usr/local/rvm/rubies/jruby-head/lib/ruby/stdlib"]
So there's no joy there, unless you explicitly add your directory to the load path. This is what Rubygems and Bundler spends most of their time doing - they manage the load paths for gems so you don't have to worry about it. However, this doesn't help you with single files.
Additionally, require_relative
will search from the directory that __FILE__
is in, but in IRB, this is a non-directory (irb)
value! This is why you get the "can't infer basepath" issue when trying require_relative
from IRB; since the currently executing file, __FILE__
, isn't a proper path, require_relative
can't figure out where to start from.
When you are not running from IRB, this isn't really an issue; require_relative 'mytest.so'
should work just fine when you execute it in a script, since the currently-executing script will populate __FILE__
. That is, if you have loader.rb
and mytest.so
and execute loader via ruby loader.rb
, require_relative
should work just fine.
If you want to run this in IRB, consider something like:
require "#{__dir__}/mytest.so"
which will expand out to the current working directory, which should by default be the directory you've launched it from. I would recommend that you not do this in a script, though, since it depends on __dir__
not having been changed, and that may be difficult to guarantee.
Ruby 'require' error: cannot load such file
I just tried and it works with require "./tokenizer"
. Hope this helps.
What is the difference between require_relative and require in Ruby?
Just look at the docs:
require_relative
complements the builtin methodrequire
by allowing you to load a file that is relative to the file containing therequire_relative
statement.For example, if you have unit test classes in the "test" directory, and data for them under the test "test/data" directory, then you might use a line like this in a test case:
require_relative "data/customer_data_1"
How to make node.js require absolute? (instead of relative)
Here is the actual way I'm doing for more than 6 months. I use a folder named node_modules as my root folder in the project, in this way it will always look for that folder from everywhere I call an absolute require:
- node_modules
- myProject
- index.js I can require("myProject/someFolder/hey.js") instead of require("./someFolder/hey.js")
- someFolder which contains hey.js
- myProject
This is more useful when you are nested into folders and it's a lot less work to change a file location if is set in absolute way. I only use 2 the relative require in my whole app.
Initialize env.rb from unit test in ruby
Assuming that the Test::Unit script directory and the cucumber project are siblings, you can use relative require
to require env.rb
. For example:
/cucumber_project/features/support/env.rb
require 'logger'
$LOG = Logger.new(STDOUT)
hello_world.rb
class HelloWorld
def say_hi
greeting = 'Hello World'
$LOG.info greeting
greeting
end
end
test.rb
require_relative 'hello_world'
require_relative '../cucumber_project/features/support/env'
require 'test/unit'
class TC_HelloWorld < Test::Unit::TestCase
def test_say_hi
assert_equal('Hello World', HelloWorld.new.say_hi)
end
end
Related Topics
Rails 5 Db Migration: How to Fix Activerecord::Concurrentmigrationerror
Ruby Variable Name with Double Underscores
How to Count Existing Instances of a Class in Ruby
How to Render File in Rails 5 API
Ruby on Rails - Link_To Button/Css
How to Serialize an Object Using Tcpserver Inside
How to Find Current Abstract Route in Rails Middware
How to Run Code After Each Line in Ruby
Gem Install Cannot Find a Header File
Ruby Dot Parenthesis Call Syntax
How to Reset a Factory_Girl Sequence
How to Use Functions Like Concat(), etc. in Arel
Ruby: How to Find the Key of the Largest Value in a Hash
How to Create Email with CSS and Images from Rails
How to Use Same Browser Window for Automated Test Using Selenium-Webdriver (Ruby)