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 working at C:\(...)\Desktop, and I need to require my scripts at G:\scripts. I tried to change the $LOAD_PATH:
$LOAD_PATH << 'G:\\scripts'
require 'filename'
and also the argument of require:
require 'G:\\scripts\\filename'
but I always get
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- dir_iterator (LoadError)
from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from a.rb:2:in `<main>'
It works well if the directory is on C:\ partition. What's happening here?
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__)
When I require a file, for example (called st.rb):
require 'rubygems'
require 'mongrel'
class TestHandler < Mongrel::HttpHandler
def process(request, response)
response.start(200) do |head, out|
head["Content-Type"] = "text/html"
out.write "Hello, World!\n"
end
end
end
in irb I get:
>> require 'st.rb'
LoadError: cannot load such file -- st.rb
from /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):3
from /usr/local/bin/irb:12:in `<main>'
I might have a clue, but it's just a guess. My ruby version/install location is:
/usr/local/bin/ruby and ruby 1.9.3p0
yet, ruby gems is in /usr/local/lib/ruby/1.9.1 and it's talking about version 1.9.1. Could this possibly be the reason?
Thanks!
UPDATE
Weird, when I type 'puts RUBY_VERSION' in IRB, I get this:
puts RUBY_VERSION
1.9.3
NoMethodError: undefined method `write' for nil:NilClass
from /usr/local/lib/ruby/1.9.1/irb.rb:311:in `printf'
from /usr/local/lib/ruby/1.9.1/irb.rb:311:in `output_value'
from /usr/local/lib/ruby/1.9.1/irb.rb:160:in `block (2 levels) in eval_input'
from /usr/local/lib/ruby/1.9.1/irb.rb:273:in `signal_status'
from /usr/local/lib/ruby/1.9.1/irb.rb:156:in `block in eval_input'
from /usr/local/lib/ruby/1.9.1/irb/ruby-lex.rb:243:in `block (2 levels) in each_top_level_statement'
from /usr/local/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `loop'
from /usr/local/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `block in each_top_level_statement'
from /usr/local/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `catch'
from /usr/local/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `each_top_level_statement'
from /usr/local/lib/ruby/1.9.1/irb.rb:155:in `eval_input'
from /usr/local/lib/ruby/1.9.1/irb.rb:70:in `block in start'
from /usr/local/lib/ruby/1.9.1/irb.rb:69:in `catch'
from /usr/local/lib/ruby/1.9.1/irb.rb:69:in `start'
from /usr/local/bin/irb:12:in `<main>'
Maybe IRB bug!
>>
The directory where st.rb lives is most likely not on your load path.
Assuming that st.rb is located in a directory called lib relative to where you invoke irb, you can add that lib directory to the list of directories that ruby uses to load classes or modules with this:
$: << 'lib'
For example, in order to call the module called 'foobar' (foobar.rb) that lives in the lib directory, I would need to first add the lib directory to the list of load path. Here, I am just appending the lib directory to my load path:
irb(main):001:0> require 'foobar'
LoadError: no such file to load -- foobar
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
from (irb):1
irb(main):002:0> $:
=> ["/usr/lib/ruby/gems/1.8/gems/spoon-0.0.1/lib", "/usr/lib/ruby/gems/1.8/gems/interactive_editor-0.0.10/lib", "/usr/lib/ruby/site_ruby/1.8", "/usr/lib/ruby/site_ruby/1.8/i386-cygwin", "/usr/lib/ruby/site_ruby", "/usr/lib/ruby/vendor_ruby/1.8", "/usr/lib/ruby/vendor_ruby/1.8/i386-cygwin", "/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/1.8", "/usr/lib/ruby/1.8/i386-cygwin", "."]
irb(main):004:0> $: << 'lib'
=> ["/usr/lib/ruby/gems/1.8/gems/spoon-0.0.1/lib", "/usr/lib/ruby/gems/1.8/gems/interactive_editor-0.0.10/lib", "/usr/lib/ruby/site_ruby/1.8", "/usr/lib/ruby/site_ruby/1.8/i386-cygwin", "/usr/lib/ruby/site_ruby", "/usr/lib/ruby/vendor_ruby/1.8", "/usr/lib/ruby/vendor_ruby/1.8/i386-cygwin", "/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/1.8", "/usr/lib/ruby/1.8/i386-cygwin", ".", "lib"]
irb(main):005:0> require 'foobar'
=> true
EDIT
Sorry, I completely missed the fact that you are using ruby 1.9.x. All accounts report that your current working directory has been removed from LOAD_PATH for security reasons, so you will have to do something like in irb:
$: << "."
The problem shall have solved if you specify your path. For example,
require 'st.rb' --> require './st.rb'
See if your problem get solved or not.
For security & other reasons, ruby does not by default include the current directory in the load_path. You may want to check this for more details - Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative?
I created my own Gem, but I did it in a directory that is not in my load path:
$ pwd
/Users/myuser/projects
$ gem build my_gem/my_gem.gemspec
Then I ran irb and tried to load the Gem:
> require 'my_gem'
LoadError: cannot load such file -- my_gem
I used the global variable $: to inspect my load path and I realized I am using RVM. And rvm has specific directories in my load path $:. None of those directories included my ~/projects directory where I created the custom gem.
So one solution is to modify the load path itself:
$: << "/Users/myuser/projects/my_gem/lib"
Note that the lib directory is in the path, which holds the my_gem.rb file which will be required in irb:
> require 'my_gem'
=> true
Now if you want to install the gem in RVM path, then you would need to run:
$ gem install my_gem
But it will need to be in a repository like rubygems.org.
$ gem push my_gem-0.0.0.gem
Pushing gem to RubyGems.org...
Successfully registered gem my_gem
I just came across a similar problem. Try
require './st.rb'
This should do the trick.
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.