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.
Related
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'
When I run a simple ls command from the Rails console, I'm getting this error:
$ rails c
irb(main):001:0> `ls`
script/rails: No such file or directory - ls
=> nil
I have the same error when using other commands such as cd ~. Can anyone tell me why the contents of the current folder are not displayed?
UPDATE:
After trying exec('ls'), this is the output, which is making me think it must be some local setting.
irb(main):001:0> exec('ls')
Errno::ENOENT: No such file or directory - ls
from (irb):1:in `exec'
from (irb):1
from /[filepath]/runtime/ruby1.9/gems/1.9.1/gems/railties-3.2.18/lib/rails/commands/console.rb:47:in `start'
from /[filepath]/runtime/ruby1.9/gems/1.9.1/gems/railties-3.2.18/lib/rails/commands/console.rb:8:in `start'
from /[filepath]/runtime/ruby1.9/gems/1.9.1/gems/railties-3.2.18/lib/rails/commands.rb:41:in `<top (required)>'
from /[filepath]/runtime/ruby1.9/1.9.1/rubygems/custom_require.rb:36:in `require'
from /[filepath]/runtime/ruby1.9/1.9.1/rubygems/custom_require.rb:36:in `require'
from script/rails:6:in `<main>'
This is because you are now actually in an interactive ruby session (notice the irb in your prompt) within the context (so you can use the class, active record models, etc.) of your ruby on rails application. Command that you issue should be ruby commands. raw shell commands don't work here as is.
However you can use exec:
$ rails c
Connecting to database specified by database.yml
Loading development environment (Rails 3.2.17)
2.0.0p247 :001 > exec('ls')
app config.ru doc Gemfile.lock log README.rdoc spec
config db Gemfile lib Rakefile script tmp
16:12:10 durrantm Castle2012 /home/durrantm/Dropnot/_/rails_apps/linker master
$
You can also use backicks (`) to run commands, i.e.
2.0.0p247 :007 > `ls`
=> "app\nconfig\nconfig.ru\ndb\ndoc\nGemfile\nGemfile.lock\nlib\nlog\nRakefile\nREADME.rdoc\nscript\nspec\ntmp\n"
Also %x:
2.0.0p247 :020 > %x('ls')
=> "app\nconfig\nconfig.ru\ndb\ndoc\nGemfile\nGemfile.lock\nlib\nlog\nRakefile\nREADME.rdoc\nscript\nspec\ntmp\n"
and system:
2.0.0p247 :021 > system("ls")
app config.ru doc Gemfile.lock log README.rdoc spec
config db Gemfile lib Rakefile script tmp
=> true
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'm scripting with Ruby 1.9.2dev in Backtrack 5 but I'm having some problems when try to parse html entities with the library "htmlentities".
I cannot load the library although I have installed it with gem.
I'll show you the problems I'm having in the console:
root#bt:~# gem list -d htmlentities
*** LOCAL GEMS ***
htmlentities (4.3.1)
Author: Paul Battley
Homepage: https://github.com/threedaymonk/htmlentities
Installed at: /var/lib/gems/1.9.2
A module for encoding and decoding (X)HTML entities.
root#bt:~# irb irb(main):001:0> require 'htmlentities' LoadError: no such file to load -- htmlentities
from (irb):1:in `require'
from (irb):1
from /usr/bin/irb:12:in `<main>'
This is the same problem I'm having with nokogiri. I installed the library with
gem install htmlentities
Do you have any idea why I'm having this problem?
Thank you.
EDITED:
I tried also with require 'rubygems' previously to any other require, but happens the same:
I tried require 'rubygems' but is happening the same:
irb(main):001:0> require 'rubygems'
=> false
irb(main):002:0> require 'htmlentities'
LoadError: no such file to load -- htmlentities
from (irb):2:in `require'
from (irb):2
from /usr/bin/irb:12:in `<main>'
Try to require 'rubygems' before the rest of your gems requirements.
rubygems is actually redefining the Kernel#require method to look for gems on your gempath. Whitout it ruby will just look for local/on path files.
It took me a lot but now I know how to fix it. It's about GEM_PATH.
# echo "export GEM_PATH=/var/lib/gems/1.9.2/" >> ~/.bashrc
# source ~/.bashrc
Now if I run irb:
# irb
irb(main):003:0> require 'htmlentities'
=> true
irb(main):004:0>
WOOT!
I used Bundler to generate a Gem skeleton for me. Within lib/foo.rb, I have the following:
require 'foo/client'
require 'foo/other'
Those two lines are supposed to require lib/foo/client.rb and lib/foo/other.rb, respectively. It builds without a problem, but when I go to test it with irb, I get a file not found error.
ruby-1.9.2-head :003 > require 'foo'
LoadError: no such file to load -- foo/client
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/ethan/.rvm/gems/ruby-1.9.2-head/gems/foo-0.1.0/lib/foo.rb:3:in `<top (required)>'
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):3
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/bin/irb:16:in `<main>'
ruby-1.9.2-head :004 >
What is the correct way to require files within the same Gem? There must be something simple that I'm overseeing...
If your gem is called 'foo', then all you need to do is use bundle exec:
bundle exec your-script.rb
Without bundle exec, the load paths are not set up correctly.
Using irb, you use the bundle command bundle console.
chris#chris:~/oss/pp-adaptive$ irb
irb(main):001:0> AdaptivePayments
NameError: uninitialized constant Object::AdaptivePayments
from (irb):1
from /home/chris/.rbenv/versions/1.9.2-p290/bin/irb:12:in `<main>'
irb(main):002:0>
chris#chris:~/oss/pp-adaptive$ bundle console
irb(main):001:0> AdaptivePayments
=> AdaptivePayments
irb(main):002:0>
Note that once the gem is installed on your system, you may use it without bundler.
The current directory is not in the load path in Ruby 1.9. Try one of these:
require './client'
or
require_relative 'client'
If you are in IRB itself you may have to
require "rubygems"
require "foo"
if the library is a gem. Alternatively you can require the full path of the gem, but I wouldn't advise it since rubygems does require magic so reload! works in irb ( at least it does for rails console ).