require: cannot load such file -- one_plus (LoadError) - ruby

I am learning custom matchers for rspec from this old, 2008 tutorial - http://www.reactive.io/tips/2008/12/10/up-and-running-with-custom-rspec-matchers/
Project Structure
.
├── one_plus.rb
└── one_plus_spec.rb
I followed all the instructions, but I am not able to understand why I am getting the following error:
rspec one_plus_spec.rb
/home/john/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- one_plus (LoadError)
from /home/john/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/john/Code/Rspec/Misc/CustomRspecMatchers/one_plus_spec.rb:3:in `<top (required)>'
from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load'
from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'
john#ubuntu:~/Code/Rspec/Misc/CustomRspecMatchers$
There is no clear answer in other stack overflow questions for this. I don't just want a solution, but I also want to know why this error happens and the concepts that I need to learn to prevent it from happening again.

Try using require_relative instead:
require_relative 'one_plus'
./ (current directory) was removed from the load path in Ruby 1.9.

your load path doesn't include ./ by default, that's why.
Understanding Ruby's load paths includes details as well as solutions.
I personally use require_relative pretty often in specs myself, but you can also modify the load path.

Related

Why gem is not loaded: require gems gives error no such file

I installed gem 'ssh-net'. From the console I tried to require the gem like this:
irb(main):009:0* require 'ssh-net'
LoadError: cannot load such file -- ssh-net
from /var/lib/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `require'
from /var/lib/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `block in require'
from /var/lib/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:240:in `load_dependency'
from /var/lib/gems/2.3.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `require'
from (irb):9
from /home/csrhub/git/csrhub-api/bin/console:150:in `<top (required)>'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/cli/exec.rb:63:in `load'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/cli/exec.rb:63:in `kernel_load'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/cli/exec.rb:28:in `run'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/cli.rb:476:in `exec'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/vendor/thor/lib/thor.rb:399:in `dispatch'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/cli.rb:30:in `dispatch'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/vendor/thor/lib/thor/base.rb:476:in `start'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/cli.rb:24:in `start'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/exe/bundle:46:in `block in <top (required)>'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/lib/bundler/friendly_errors.rb:123:in `with_friendly_errors'
from /var/lib/gems/2.3.0/gems/bundler-2.1.1/exe/bundle:34:in `<top (required)>'
from /usr/local/bin/bundle:23:in `load'
from /usr/local/bin/bundle:23:in `<main>'
Then I tried with a gem that works:
irb(main):023:0* require 'memcache'
=> false
I printed the gems locations:
csrhub#csrhub:~/git/csrhub-api$ bundle info 'net-ssh'
* net-ssh (5.2.0)
Summary: Net::SSH: a pure-Ruby implementation of the SSH2 client protocol.
Homepage: https://github.com/net-ssh/net-ssh
Path: /var/lib/gems/2.3.0/gems/net-ssh-5.2.0
csrhub#csrhub:~/git/csrhub-api$ bundle info 'memcache'
* memcache-client (1.8.5)
Summary: A Ruby library for accessing memcached.
Homepage: http://github.com/mperham/memcache-client
Path: /var/lib/gems/2.3.0/gems/memcache-client-1.8.5
csrhub#csrhub:~/git/csrhub-api$
I see that both gems are placed in the same directory. Why memcache is invokable and the ssh-net is not? What am I missing?
May be it has something to do with this ?
I installed gem 'ssh-net'.
No, you didn't. You installed the gem net-ssh, as you can clearly see in the output of bundle info that you posted:
csrhub#csrhub:~/git/csrhub-api$ bundle info 'net-ssh'
* net-ssh (5.2.0)
Summary: Net::SSH: a pure-Ruby implementation of the SSH2 client
Here you can see that the name of the gem is net-ssh and not ssh-net and that the name of the primary class is Net::SSH.
From the console I tried to require the gem like this:
irb(main):009:0* require 'ssh-net'
LoadError: cannot load such file -- ssh-net
The primary class in this gem is called Net::SSH. (It integrates in the Net namespace of the Ruby standard library, which has libraries like Net::FTP, Net::HTTP, Net::IMAP, Net::POP, Net::SMTP, and Net::Telnet.)
According to standard Ruby naming conventions, a class named Net::SSH should be located in a file named lib/ssh.rb. This is the file you need to require.
This is also shown in the very first line of the very first code sample of the very first page of the documentation:
require 'net/ssh'
I see that both gems are placed in the same directory. Why memcache is invokable and the ssh-net is not? What am I missing?
In 99.9% of all cases where a computer tells you it cannot find something, it is because that thing you told it to find is indeed not there. Computers are very, very good at finding things.
The same thing is the case here: you told Ruby
require 'ssh-net'
which means "go through each directory in the $LOAD_PATH and look for a file named ssh-net.rb". Such a file simply does not exist, ergo, you get a LoadError exception.

Running a single RSpec spec fails with "`require': cannot load such file"

I am trying to get test-first-ruby-master as instructed but I am getting multiple errors. This is the structure of the files/directory in test-first-ruby-master:
Gemfile Gemfile.lock README.md lib spec
lib contains ruby files and spec contains spec files.
I am trying to execute 00_hello_spec.rb file on my terminal but it doesn't work.
What I have tried
require "lib/00_hello.rb"
require_relative "lib/00_hello.rb"
Errors I get
supritkumars-MacBook-Pro:test-first-ruby-master supritkumarshah$ rspec spec/00_hello_spec.rb
/Users/supritkumarshah/Desktop/test-first-ruby-master/spec/00_hello_spec.rb:103:in `require': cannot load such file -- lib/00_hello.rb (LoadError)
from /Users/supritkumarshah/Desktop/test-first-ruby-master/spec/00_hello_spec.rb:103:in `<top (required)>'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `load'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `each'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:97:in `setup'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:85:in `run'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/exe/rspec:4:in `<top (required)>'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `load'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `<main>'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
Thank you
The require is failing because you're requiring a relative path, 'lib/00_hello.rb', but the directory it's relative to (the root of your project) is not in the list of directories that Ruby is looking in for files to require (the load path, $LOAD_PATH).
There are lots of ways to fix this. Here are some:
Tell rspec to add the root of your project to the load path. Since that's your current directory, you can use .:
rspec -I. spec/00_hello_spec.rb
Change your spec to require_relative '../lib/00_hello'. This works regardless of the current directory or load path — a good idea in command-line programs, although not so important for a learning project.
As B Seven answered, change your spec to require './lib/00_hello'. This requires you to run rspec in the root of your project.
You can even remove the require altogether! When you run rspec, if the current directory has subdirectories named lib and/or spec, rspec adds them to the load path. This also requires you to run rspec in the root of your project. This solution won't work if you need the require when running your program for real (not under rspec).
Note that including the .rb in the filename you required did not cause the problem, but it's not necessary.
Although Dave's answer is correct, I prefer to use:
require './lib/00_hello'
Did you tried adding require 'bundler/setup' at the top of your test helper file?

Error When running RSpec in Ruby App Created through IRB

I'm working on an exercise that gets me started on Test Driven Development Using RSpec
I've been following the instructions and I've created a file repository on GitHub, and performed gem install rspec. However, when I try to run commands to test RSpec is functioning it throws this massive error at me and I haven't been able to find out why. I'm somewhat used to using Ruby on Rails, but this is my first application outside the rails framework.
Here's the error:
/Users/jrshafer/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `load': cannot load such file -- /Users/jrshafer/bloc/code/address-bloc/specs/entry_spaces.rb (LoadError) from /Users/jrshafer/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files' from /Users/jrshafer/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `each' from /Users/jrshafer/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `load_spec_files' from /Users/jrshafer/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:97:in `setup' from /Users/jrshafer/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:85:in `run' from /Users/jrshafer/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run' from /Users/jrshafer/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke' from /Users/jrshafer/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.2.3/exe/rspec:4:in `<top (required)>' from /Users/jrshafer/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `load' from /Users/jrshafer/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `<main>' from /Users/jrshafer/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval' from /Users/jrshafer/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'
I'm usually pretty good at finding fixes for any kind of error during development but this one has me stuck. I'd appreciate any help you could give.
So, after staring at it for a while and comparing it to an example I realized I missed a case sensitive Entry in the RSpec.describe. Noob mistake.

time_zone.rb:270: warning: circular argument reference - now

I'm using Ruby on Rails, and I installed all the necessary applications and updates for gems. I already exhausted/researched most of all possible answer for this error and tried all of it, but still am having no luck.
/Users/u=Username/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-3.2.13/lib/active_support/values/time_zone.rb:270: warning: circular argument reference - now
/Users/Username/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.8.5/lib/bundler/runtime.rb:76:in `require': cannot load such file -- false (LoadError)
from /Users/Username/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.8.5/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
from /Users/Username/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.8.5/lib/bundler/runtime.rb:72:in `each'
from /Users/Username/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.8.5/lib/bundler/runtime.rb:72:in `block in require'
from /Users/Username/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.8.5/lib/bundler/runtime.rb:61:in `each'
from /Users/Username/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.8.5/lib/bundler/runtime.rb:61:in `require'
from /Users/Username/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/bundler-1.8.5/lib/bundler.rb:134:in `require'
from /Users/Username/Downloads/job4quote/config/application.rb:7:in `'
from /Users/Username/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/railties-3.2.13/lib/rails/commands.rb:53:in `require'
from /Users/Username/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/railties-3.2.13/lib/rails/commands.rb:53:in `block in '
from /Users/Username/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
from /Users/Username/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/railties-3.2.13/lib/rails/commands.rb:50:in `'
from script/rails:6:in `require'
from script/rails:6:in `'
Is there a way to access these .rb files to make necessary changes?
As shown here -> warning: circular argument reference
The problem seems to be caused by the higher version of ruby (In my case rails 3.2.13 and ruby 2.2) and using an older version of rails. One solution in which worked for me was to use ruby 2.0 and to update my gem file (RVM Capistrano).
gem 'rvm-capistrano', require: false
run the bundle install command. After that everything worked as expected.
silly of me, you can access the file by browsing it thru terminal
$ open .rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-3.2.13/lib/active_support/values
and made changes on that file as shown on the link about.
Sorry new with ruby.

What does "<top (required)>" mean in a Ruby stack trace?

In Ruby 1.9.2 stack trace I frequently see the method given as <top (required)>, as in this section of stack below. What does this mean? Is my Ruby install subtly broken?
Could not find abstract-1.0.0 in any of the sources
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/spec_set.rb:87:in `block in materialize'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/spec_set.rb:81:in `map!'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/spec_set.rb:81:in `materialize'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/definition.rb:90:in `specs'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/definition.rb:135:in `specs_for'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/definition.rb:124:in `requested_specs'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/environment.rb:23:in `requested_specs'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/runtime.rb:11:in `setup'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler.rb:107:in `setup'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/setup.rb:14:in `<top (required)>'
<internal:lib/rubygems/custom_require>:33:in `require'
<internal:lib/rubygems/custom_require>:33:in `rescue in require'
<internal:lib/rubygems/custom_require>:29:in `require'
It's the top level of a file i.e. whatever gets run when the file is required.
So if something fails during the setup of a library (for example some required file isn't found) it will show up in the stacktrace like that.
If everything you required is correct, it could mean that you're trying to create a class with a name that already exists for a module. For example the following file :
class Test
end
Will raise :
<top (required)>': Test is not a class (TypeError)
Because Test is implicitely a module.
I ran into this error of <top (required)> when I was doing the tutorial in the book "Jump Start Sinatra."
I got rid of the error by making sure that I ran sudo gem install <GEM_IN_YOUR_FILE>. So in my case I had a main.rb and in that file I had this
require 'sinatra'
require 'sinatra-contrib'
So I went back into the root of my project and ran sudo gem install sinatra and sudo gem install sinatra-contrib and then my project worked fine.
Your errors will vary, but because this is what I found when I searched on Google, I know others will come here for similar reasons. And I offer this solution to at least get you thinking in the right direction since this worked for me.
I had the same problem. Solved it by converting .rb files encoding to UTF-8-BOM using Notepad++.
Ninto's answer makes total sense. It's a stack trace that you get one things fail. I was running my tests by doing:
bundle exec rspec spec
and what I got was:
An error occurred while loading ./spec/foodie_spec.rb.
Failure/Error: require 'foodie/food'
SyntaxError:
/Users/honey/Dev/foodie/funnex/lib/funnex/food.rb:17: syntax error, unexpected end-of-input, expecting end
# ./lib/foodie.rb:4:in `require'
# ./lib/foodie.rb:4:in `<top (required)>'
# ./spec/foodie_spec.rb:1:in `require'
# ./spec/foodie_spec.rb:1:in `<top (required)>'
No examples found.
Finished in 0.00002 seconds (files took 0.07998 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
All I was doing wrong in my case was that I was missing and end.
But to understand the stack trace more, start from bottom of the commented out lines ie this line:
# ./spec/foodie_spec.rb:1:in `<top (required)>'
At line 1 of foodie_spec I had require 'foodie'
At line 4 of foodie.rb I had require 'foodie/food'
I errored out at line 17 of foodie/food
Stack traces in Ruby are also named backtraces or stack traceback

Resources