Why is autoload failing to load files for gems - ruby

I am trying to read emails in ruby using this gmail gem.
When I require 'gmail' in IRB or in a script, I get this error:
/Library/Ruby/Gems/1.8/gems/gmail-0.4.0/lib/gmail.rb:70:in connect_with_proper_client': no such file to load -- gmail/client (LoadError)
from /Library/Ruby/Gems/1.8/gems/gmail-0.4.0/lib/gmail.rb:48:in new
This is happening because autoload cannot file the 'gmail/client' file.
When I add require 'gmail/client' manually, the problem goes away till the next autoload call. This solution is unacceptable because I cannot anticipate which files to add in advance.
I found a similar question, however it didn't remedy my problem.

Related

RubyGems - require, file location and (load error) complications

very new to coding so, having exhausted Google and Stack Overflow, would really appreciate some advice...
I am currently building a web-scraper to get familiar with CMD vs Sublime Text, feeling Ruby in action; So i am working my way through this tutorial
After having actioned in CMD
C:\gem install HTTParty
SUBLIME TEXT - starts with this code:
require_relative 'HTTParty'
require_relative 'Nokogiri'
etc
But before i can get to anything more from CMD, i hit web_scraper.rb and it returns with:
C:/Users/ATH18/Desktop/nokogiri_tutorial/web_scraper.rb:1:in `require_relative': cannot load such file -- C:/Users/ATH18/Desktop/nokogiri_tutorial/httparty (LoadError)
from C:/Users/ATH18/Desktop/nokogiri_tutorial/web_scraper.rb:1:in `<main>'
[Finished in 0.1s with exit code 1]
I think this has to be due to one of the following:
i) maybe gems have to have their actual files dragged into whatever folder you're creating a new program in?
ii) i'm missing another piece of information that would let it run properly?
iii) perhaps there's another way to tell CMD/ruby that the "require"d gem is not in the current folder (I read this somewhere but their advice didnt seem to work either).
NOTE - i have done gem install xxxxxx in both the C:\ directory and C:\users\desktop\projectFolder\
Help?
You have to use require instead of require_relative. The difference between both a is explained here: https://stackoverflow.com/a/3672600/92049
Use require 'GEMNAME' for gems installed with gem install GEMNAME; use require_relative 'PATH' to require a file relative to the file containing require_relative. (Most often you will find yourself using require.)
To come back to your question: As it says in the tutorial, you have to write require 'HTTParty' instead of require_relative 'HTTParty'.
Does this answer your original question?

LoadError when requiring a renamed gem

I started building Ruby bindings for the Marvel Comics API earlier this month. At the time, there was no gem named marvel on RubyGems, so I fired up jeweler, created a project, and began making a crude, but useable first release. I tested it by rake installing it locally and requiring it in a dummy project that let me play with it in pry:
require 'marvel'
require 'dotenv'
require 'pry'
Dotenv.load
#client = Marvel::Client.new
#client.configure do |config|
config.api_key = ENV['API_KEY']
config.private_key = ENV['PRIVATE_KEY']
end
binding.pry
When I got to the point where I had exposed several endpoints (at this commit) I attempted releasing it to rubygems.org, but discovered someone by then had released a marvel gem. I hastily altered my Rakefile and renamed mine to marvel_api and released it.
I let it sit for a few days before I came back and started experimenting with adding in Faraday middleware to try and clean it up. However, I never seem to have tested whether the name change to marvel_api worked. Now, whenever I try to require marvel_api, I'm met with this LoadError:
/Users/Raevynheart/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- marvel_api (LoadError)
from /Users/Raevynheart/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from test.rb:1:in `<main>'
I'm trying to understand if this is happening because my process for renaming the gem was incorrect, or if this is some separate issue. The gem's source is here: https://github.com/O-I/marvel. Note that the repo name and gem name are different — I don't know if that is an issue. Let me know if there is any other information I need to add to help troubleshoot this. Thanks for any help!
I think you are facing this issue since within your gem's lib directory, you still have the file named as marvel.rb.
From http://guides.rubygems.org/make-your-own-gem/ :
Code for your package is placed within the lib directory. The convention is to have one Ruby file with the same name as your gem, since that gets loaded when require 'hola' is run. That one file is in charge of setting up your gem’s code and API.
So, I believe your issue will resolve by changing your filename within lib to marvel_api.rb.

Can't get RSpec to work -- 'require': cannot load such file

I just spent three days of my life banging my head against the wall trying to figure out why a simple 'rake' would not pass my spec file.
If this happens to you: Do not have a space in any folder path!. Seriously. In fact do not have a space in anything you name from here on out.
Here is my console output:
(in /Users/*****/Desktop/Learning Ruby/learn_ruby)
$ rake
/Users/*******/Desktop/Learning Ruby/learn_ruby/00_hello/hello_spec.rb:116:
in `require': cannot load such file -- hello (LoadError)
The failure is caused by the line: require "hello"
This line tells Ruby that it needs to search the load path for a file named hello.rb. However, when it looks at the load path, it can't find that file. You should either remove that line and define your code directly in the spec file, or create a hello.rb file.
Newer versions of RSpec (2.11+ I believe) automatically add subdirectory lib to the load path. Based on your Rakefile it seems you are also loading the current lab directory and the subdirectory solution.
I'm guessing you're expected to put your solution in solution/hello.rb.
What worked for me was changing the require statement to a require_relative
I am using windows and an IDE

Fixing "custom_require.rb:36:in `require': no such file to load" errors

I understand that after Ruby 1.9.2, '.' is no longer in your path for security reasons. This seems to be a problem when using certain gems (ones not updated to 1.9 I imagine?), a problem that throws errors like
$HOME/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in 'require': cannot load such file -- rubylog (LoadError)
I've seen and fixed this problem once, by (perhaps naively) changing some requires into require_relatives: https://github.com/mathpunk/MongoDB_Koans/commit/e2f7898347d328450ec121d22f701508f389cc53
Now I'd like to use rubylog, and I'm getting the custom_require error, so I tried the same trick:
https://github.com/mathpunk/rubylog/commit/995e13dccc6a197d280d0783f3fb7fe50deabd02
but this time, I'm just getting the same error. What else can I try?
ETA: All this time, I've been using sudo gem install blah to install gems, and for some reason, for rubylog it's gem install rubylog that does it. (Something to do with RVM?) So now everything works. Thank you.
Your code fails at require 'rubylog' - so it can't find rubylog.rb itself. So just add dir containing rubylog.rb to load path - something like $: << 'rubylog' might help.
Just add the library directory to your LOAD_PATH:
$LOAD_PATH.unshift(File.dirname(__FILE__))
See Understanding Ruby's load paths\
Edit: I assumed you had a rubylog directory wherever your script was running from. If your script can't find rubylog then you need to add that location to your load path:
$LOAD_PATH.unshift('/path/to/rubylog')
Are you sure you have the rubylog libraries? gem install rubylog

ruby require not working

I'm new to ruby, but I'm working on my first ruby program. It currently has two files, one is a library of functions (xgync.rb stored in lib) the other is the executable xgync stored in 'bin'. (Project visible here https://bitbucket.org/jeffreycwitt/xgync/src) I've also created a symlink to my /usr/local/bin/xgync so that I can write the command xgync {arguments} from anywhere in the terminal.
The problem seems to be that bin/xgync depends on the library lib/xgync.rb. I've written this dependency in bin/xgync as follows:
$:.unshift(File.dirname(__FILE__) + '/../lib')
require "xgync"
However, i keep getting the following error:
/Users/JCWitt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- xgync (LoadError)
from /Users/JCWitt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/bin/xgync:4:in `<main>'
can you see anything wrong with what I've written? Could the symlink be somehow messing things up?
Thanks for your help :)
When using ruby 1.9.x you don't usually alter the path with the $:.unshift when requiring other files in your project.
Instead the best practice is to use require_relative instead.
require_relative '../lib/xgync.rb'
require_relative requires files relative to the file you are currently editing.
But the error you experience appears, because you require a file, which does not exist:
bin/xgync
lib/xgync.rb
These are the files in your project according to your question, and the code-excerpt is from bin/xgync you extended the path to look for files in lib/ but you try to require 'xgync' which is a file, that is not present in lib/, so if you wanted to use this method (instead of require_relative you would have to use require 'xgync.rb'.

Resources