How to fix "`require_relative': cannot load such file" cannot load - Ruby - ruby

Tried to solve this on my own and so far no luck...
Error below:
1: from bin/run:3:in `<main>'
bin/run:3:in `require_relative': cannot load such file -- /Users/jason/Development/code/First Project/bin/bin/environment.rb (LoadError)
What's in my bin folder:
#!/usr/bin/env ruby
require_relative './bin/environment.rb'
CLI.new.call
In my config folder:
require 'pry'
require 'httparty'
require_relative "./lib/api.rb"
require_relative "./lib/cli.rb"
require_relative "./lib/pokemon.rb"
Project Directory:
-->Firstproject
->bin
-run
->config
-environment.rb
->lib
-api.rb
-cli.rb
-pokemon.rb
-gitignore
-Gemfile
-README.md

I think you just need to adjust the path to correctly reference your environment.rb source file:
#!/usr/bin/env ruby
require_relative '../config/environment.rb'
CLI.new.call
If you look closely where your environment.rb file lives, it is in the project_dir/config/environment.rb but your bin/run you are attempting to locate the file at ./bin/environment.rb which doesn't exist according to your Project Directory layout.

Check that your environment.rb file lives in config folder, not in lib. So change it to
require_relative './config/environment.rb'

Related

Unable to include file in the spec file

I am trying to write an RSpec test for a ruby project(Sketchup plugin) but I am facing an issue with the require.
Below is how my folder is structured
In my smoke_tests_spec.rb
require "main_folder/subfolder1/file_to_test.rb"
When I run the rspec using rspec-core/rspec from the root directory I get the following error
Failure/Error: require "main_folder/subfolder1/file_to_test.rb"
LoadError:
cannot load such file -- main_folder/subfolder1/file_to_test.rb
You need to change the LOAD_PATH or use require_relative
Here you have more info.

Why I am getting LoadError for require 'support/number_helper' in Ruby

When I am using the require_relative 'support/number_helper' it's working fine, But when I am using require 'support/number_helper' then I am getting this error.
rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- ./support/number_helper (LoadError)
I also tried this but getting the same error.
require './support/number_helper
I am using the ubuntu.
require_relative 'support/number_helper' searches for a file to load by adding given string to the directory of current_file (__FILE__). For example, in your project folder you have 2 files:
lib/special_gem/fetcher.rb
lib/special_gem/support/number_helper.rb
You can use require_relative 'support/number_helper' in your 1st file to load the 2nd. The command takes the path to the directory of the current file (lib/special_gem/), appends given string (support/number_helper) and successfully finds file to load.
What about require command, if given path is not absolute, it will search for the file in the directories listed in $LOAD_PATH. Very likely your lib folder is in this list, so to load 2nd file you could use the command
require 'special_gem/support/number_helper'
Since it's not relative, you can use it from your 1st file or any other file of your project.
When using require 'support/number_helper' it will search for the file at lib/support/number_helper. If that file is missing, LoadError exception is raised.
See the documentation for details.

Environment configuration for Sinatra error

I have an environment config file at config/environment.rb with the following:
require 'rubygems'
require 'bundler'
Bundler.setup
require 'sinatra'
require 'sinatra/base'
require 'sinatra/reloader'
and in my config.ru I have:
require File.expand_path('../config/environment', __FILE__)
require 'slim'
require 'coffee-script'
require 'padrino-helpers'
require 'sinatra/twitter-bootstrap'
I am getting the error :
Errno::ENOENT at /profile
No such file or directory - /Users/myusername/projects/accounts/config/views/profile.slim
and this only goes away when I remove require 'sinatra' from the config/environment.rb file and into config.ru. Can anyone explain why this happens? I assumed that the require File.expand_path('../config/environment', __FILE__) will simply include all requires from that file into config.ru but that doesn't seem to be the case. It now thinks my views live inside the config folder.
I followed the suggestion given here: How do I make Rake tasks run under my Sinantra app/environment? but again, moving require 'sinatra' into the environment breaks the app.
The error looks like it is from Sinatra being unable to find a Slim template when rendering a response, because it’s looking for a views directory under the config directory. By default Sinatra looks for the views dir relative to the application file, which (again by default) is the file that calls require 'sinatra'. In your case the require line is in config/environment.rb so Sinatra treats that as the app file and looks for the views dir below it.
I’m assuming you have an actual application file that you haven’t shown. The simplest solution is probably to explicitly set the application file setting in there:
set :app_file, __FILE__
Depending on your setup you might want to specify the view directory directly instead:
set :views, 'path/to/views'

Heroku load error on require?

My Ruby application runs fine on my nitrous.io box, but when I push it to Heroku and it attempts to run a scheduled process, the logs show this error:
2013-12-23T22:37:11.902160+00:00 heroku[scheduler.4283]: State changed from starting to up
2013-12-23T22:37:12.178751+00:00 app[scheduler.4283]: from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require'
2013-12-23T22:37:12.178751+00:00 app[scheduler.4283]: from /app/bin/rbtc:3:in `<main>'
2013-12-23T22:37:12.178751+00:00 app[scheduler.4283]: /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require': cannot load such file -- rbtc_arbitrage (LoadError)
2013-12-23T22:37:13.432972+00:00 heroku[scheduler.4283]: Process exited with status 1
2013-12-23T22:37:13.461438+00:00 heroku[scheduler.4283]: State changed from up to complete
This is the code in /app/bin/rbtc:3
#!/usr/bin/env ruby
require 'rbtc_arbitrage'
RbtcArbitrage::CLI.start ARGV
File structure link
I tried changing this to require_relative as in a answer to someone else on Stack Overflow to no avail.
I'm kinda at a loss here. Any help is appreciated!
Please, make sure that this file exists: lib/rbtc_arbitrage.rb which loads other files in your repo like this (syntax is valid if you are using bundler):
require 'rbtc_arbitrage/version'
require 'rbtc_arbitrage/file1'
# .. and so on
Now, adding this file should work alone, but if this does not work, try adding your lib directory to the LOAD PATH in your bin/rbtc file before any require statments, like this:
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
This explicitly tells ruby to add the lib directory to the load path, and should solve your problem.

Ruby 'require' unable to load such a file

I want to require file all_class from file log_in_test.rb.
I tried all kinds of require and require_relative, for example
#require "../../ib4b-template/features/page-object/all_class"
require_relative "page-object/all_class"
and they are not working. Can anyone kindly give me a hand?
Try this:
require_relative 'all_class' # without 'page-object/'
The most common way to solve this problem is using File.dirname(__FILE__):
require File.join(File.dirname(__FILE__), '..', 'all_class')
Use
require_relative '../page_object/all_class'
relative to support/log_in_test.rb, all_class.rb lies in above path.
require doesn't find if the directory structure is not in the Ruby's library path.
require_relative './page_object/all_class.rb'
or
require_relative './page_object/all_class'
the '.rb' part is optional
You should use require_relative instead of require when you are working on a project and want to require one file from one of the directories into another.
You should use require to add gem libraries to your project.
Also, a good practice to use when working on a larger practice is to make a environment.rb file in the config folder and have that require all the the models and then having the executables requiring that environment file.
require File.join(File.dirname(__FILE__), '..', '..','page-object', 'all_class')

Resources