Here is my project structure:
/app
--/lib
----/porter.rb
--/spec
----/porter_spec.rb
In file porter_spec.rb i have include directive:
require '../lib/porter'
Now I'm trying to run the test:
cd app
rspec
and get the error:
C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- ../lib/porter (LoadError)
from C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from C:/Dropbox/development/myprojects/lj-parser/spec/porter_spec.rb:3:in `<top (required)>'
How can I require file on lib folder?
Apparently, your current dir is not what you think it is.
You should use require_relative (ruby 1.9+):
require_relative '../lib/porter'
In ruby1.9, you can use:
require_relative '../lib/porter'
In ruby1.8 or higher, you can use:
require File.expand_path("../lib/porter", __FILE__)
Related
I have the following ruby code:
require 'locationclass'
I have installed the gem locationclass, but it's still giving me the error message:
LoadError: cannot load such file -- locationclass
from C:/Ruby/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from C:/Ruby/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from (irb):1
from C:/Ruby/bin/irb.cmd:19:in `<main>'
Does anyone know how to fix this?
require command loads files, not gems. As I see the gem "locationclass" has only one file in its lib folder, named main.rb. So to load it you need to call:
require 'main'
Also, it's a bad style to have different names for gem and its main file. Not to mention that name 'main' is too generic.
I have following file structure
xml_parse
- files
-sitemap
- parse_sitemap.rb
Here is the code in parse_sitemap.rb
require 'nokogiri'
require './files/sitemap'
doc = Nokogiri::XML(File.open('./files/sitemap'))
puts doc.xpath("//loc")
Here is the error
/home/vamsi/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- ./files/sitemap (LoadError)
from /home/vamsi/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from parse_sitemap.rb:2:in `<main>'
Remove require './files/sitemap', the require method is only for Ruby files and libraries.
I'm with the following problem:
I'd like to require a 'config/application.rb' file to my index.rb.
In my task, I have to use pure Ruby.
config/application
Dir["app/models/*.rb"].each do |file|
require_relative file
end
Dir["app/importers/*.rb"].each do |file|
require_relative file
end
index.rb
require 'config/application'
contas_endereco = ARGV[0].to_s
transacoes_endereco = ARGV[1].to_s
conta_arquivo = Arquivo.new(contas_endereco)
transacoes_arquivo = Arquivo.new(transacoes_endereco)
transacoes_importer = TransacoesImporter.new(conta_arquivo, transacoes_arquivo)
transacoes_importer.importar
But I got this error:
/home/kelvin/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- config/application (LoadError)
from /home/kelvin/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from index.rb:1:in `<main>'
I tried to use require_relative too, but I got the following error:
/home/kelvin/workspace-ruby/desafio-dinda/config/application.rb:2:in `require_relative': cannot load such file -- /home/kelvin/workspace-ruby/desafio-dinda/config/app/models/transacao.rb (LoadError)
from /home/kelvin/workspace-ruby/desafio-dinda/config/application.rb:2:in `block in <top (required)>'
from /home/kelvin/workspace-ruby/desafio-dinda/config/application.rb:1:in `each'
from /home/kelvin/workspace-ruby/desafio-dinda/config/application.rb:1:in `<top (required)>'
from index.rb:1:in `require_relative'
from index.rb:1:in `<main>'
require 'config/application'
In Ruby, files included with Kernel#require must generally be in the $LOAD_PATH or given as relative paths, and don't include the filename extension. Some examples include:
Add the script's directory to your $LOAD_PATH.
$:.unshift File.dirname(__FILE__)
require 'config/application'
Require a file relative to the current working directory.
require './config/application'
You might also use Kernel#load with an absolute path. For example:
load '/path/to/config/application.rb'
I've done some research and nothing quite hits on my issue...
I'm building a gem so I have a directory structure like this
root/ - lib/ - mygem/ - cli.rb
- version.rb
- xmltemplates.rb
- mygem.rb
- bin/
It's a thor app so in cli.rb I have:
require 'thor'
require 'mygem/version'
require 'mygem/xmltemplates'
module MyGem
#STUFF
end
And in vesrion.rb:
module MyGem
VERSION = '0.1.0'
end
and in xmltemplates.rb:
module MyGem
MY_TEMPLATE = 'TEST'
end
I was getting errors when trying to compile with as a gem so I decided to play with it in irb.
So for this test I did cd lib to make myself local to the lib directory.
1.9.3-p392 :001 > require 'rubygems'
=> false
1.9.3-p392 :003 > require 'mygem'
=> true
1.9.3-p392 :005 > require 'mygem/cli'
LoadError: cannot load such file -- mygem/xmltemplates
from /Users/user/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/user/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/user/.rvm/gems/ruby-1.9.3-p392/gems/mygem-0.1.0/lib/mygem/cli.rb:3:in `<top (required)>'
from /Users/user/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/user/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):5
from /Users/user/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
1.9.3-p392 :007 > require 'mygem/xmltemplates'
LoadError: cannot load such file -- mygem/xmltemplates
from /Users/user/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/user/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):7
from /Users/user/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
1.9.3-p392 :009 > require 'mygem/version'
=> true
1.9.3-p392 :010 > MyGem::VERSION
=> "0.1.0"
So it boils down to this: Is there any reason that mygem/version would load find and mygem.xmltemplates would not? I've checked permissions on the files as well and they are all identical.
Some of the previous posts I've read mentioned require_relative, but that didn't work for me and it seems like if that was it I would not have been able to load cli.rb or version.rb.
When working with gems it's important that your .gemspec file is up to date. The gem loader uses this to find files. Perhaps you haven't added xmltemplates to that spec yet?
If you're trying to diagnose loading problems, always check $LOAD_PATH to be sure your lib/ is in there. If it isn't, you will get LoadError type exceptions due to missing files.
I am trying to include a Ruby module.
In the file helper.rb, I have this text
module Helper
...
end
In the file test.rb, I have this text:
....
require 'helper'
...
These files are on the same level of the directory yet I keep getting this error:
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- helper (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from test.rb:4:in `<main>'
I have also tried
include Helper
in test.rb and get this error:
test.rb:4:in `<main>': uninitialized constant Object::Helper (NameError)
What am I doing wrong?
In Ruby 1.9 you should use
require_relative 'helper'
Try require './helper'. That should do it.