Require order in a ruby script - ruby

I have a ruby script that requires 3 gems to work. My script starts as follows:
#!/usr/bin/env ruby
require 'httparty'
require 'imgkit'
require 'twitter'
Now, interestingly, the above code works, but only if I require httparty first or second. If I require it as the third dependency, I get the following:
'require': cannot load such file -- httparty (LoadError)
I'd love to learn why this is happening, so I can better understand how ruby handles gem dependencies. Many thanks!
Edit: I am using bundler. This is my Gemfile:
source 'https://rubygems.org'
ruby '2.2.2'
gem 'wkhtmltoimage-binary'
gem 'imgkit'
gem 'twitter'
gem 'rspec'
gem 'httparty'

Following tadman's advice, adding require 'bundler/setup' solved the problem. More in the docs. Thanks!

Related

'Bundler.require' requiring all gems?

Trying to figure out Ruby's Bundler library. My understanding is we can require only certain groups, but my setup seems to be loading gems specified in other groups as well.
Gemfile
source 'https://rubygems.org'
# Specify your gem's dependencies in apple.gemspec
gemspec
group :production do
gem 'mail'
gem 'bundler'
gem 'pry'
gem 'commander'
gem 'fastlane'
gem 'spaceship'
gem 'highline'
gem 'terminal-table'
gem 'clipboard'
gem 'date'
gem 'mysql2'
gem 'fileutils'
gem 'redis'
gem 'json'
gem 'logger'
gem 'jira-ruby', :require => 'jira-ruby'
end
group :jenkins do
gem 'terminal-table'
gem 'pry'
gem 'mail'
gem 'jira-ruby'
gem 'spaceship'
end
test.rb
require 'pry'
binding.pry
require 'rubygems'
require 'bundler/setup'
Bundler.require(:jenkins) # I want this step to require only gems listed under 'jenkins' group in `Gemfile`.
...
When I run the code, it seems Bundler.require(:jenkins) step seems to be requiring all gems specified in Gemfile,
I am still in the process of understanding Bundler, pardon me if the question I asked is too obvious. Does anyone know how to only load the gems from bundler groups? Thanks in advance!!
This is kind of confusing, but as far as I can tell from using it myself, it only requires the gems in the given group even though it lists all of them.
It's pretty easy to see this for yourself, just try using one from the production group
Bundler.require(:jenkins)
Date.new # Should error
Really it seems like you may have noticed this yourself, but you just didn't realize.. If it was including all of them you wouldn't have needed to require pry in test.rb :)

Bundler how to require gems separately in Gemfile just in one file?

I have a conflict between rspec and mocha (rspec is not using mocha but other minitest tests are).
If I put mocha in my gemfile (even with require: false) it gets loaded by activesupport/test_case.rb:15
silence_warnings { require 'mocha/setup' }
which then causes rspec to barf.
So, I'd like to just require it in my test_setup file from my system gems but I can't figure out how to load a gem outside of bundler.
Other ideas on how to get these gems to play nice are welcome.
You can use groups in your Gemfile: http://bundler.io/v1.3/groups.html
Require the gems in particular groups, noting that gems outside of a
named group are in the :default group
Bundler.require(:default, :development)
Require the default gems, plus the gems in a group
named the same as the current Rails environment
Bundler.require(:default, Rails.env)
Restrict the groups of gems that
you want to add to the load path. Only gems in these groups will be
require'able
require 'rubygems'
require 'bundler'
Bundler.setup(:default, :ci)
require 'nokogiri'
Could you just install gem and require it?
$ gem install my_gem
and in testfile:
require 'full/path/to/my_gem.rb'

Ruby: require downloaded gems

I have finished writing a ruby script which I would like to share with others, but I am having trouble getting the user to install the necessary gems. I have tried 2 approaches, and a fix for either of them would be greatly appreciated! I require the following gems:
require 'rubygems'
require 'highline/import'
require 'mechanize'
I have tried the following:
1) Generate a stand-alone app with Platypus. I created the Gemfile:
source "https://rubygems.org"
gem "highline", "~> 1.6.20"
gem "mechanize", "~> 2.7.3"
and bundle installed it and included require 'bundler/setup'. I uploaded Gemfile.lock and the ruby script but I receive this error when I run it:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in require': cannot load such file -- highline/import (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:inrequire'
from /Users/jonathanli/Documents/CS_projects/iSites/Isites.app/Contents/Resources/script:5:in `'
2) I downloaded the gem files and placed them directly in my host folder. I am unsure of how to modify my require statements, but it is not working as it stands right now.
Thanks in advance everyone! Hope this was enough detail.

Bunder require yaml/logger

I am using bundler to require all the gems in my project. However, it's not working for yaml/logger.
If I add gem 'yaml' to my gemfile, and run bundle install, I get:
Could not find gem 'yaml (>= 0) ruby' in the gems available on this machine.
But I require it normally just fine. What am I doing wrong?
Thanks
YAML is part of the Ruby Standard Library, and not a Gem.
You do not need to add it to your Gemfile, just require it.
irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> YAML
=> Psych
The same applies for Logger.

How can I use Nokogiri in irb?

I am trying to:
require 'nokogiri'
in irb, without success. The Nokogiri gem is installed. From:
gem list --local
I get:
nokogiri (1.4.4, 1.4.3.1)
but when I try to "require" it in irb, I get:
LoadError: no such file to load -- nokogiri
from (irb):8:in `require'
from (irb):8
from :0
Nokogiri 'lives' in:
/opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/gems/1.8/gems/nokogiri-1.4.4/lib
on my system. Also, my GEM PATH (from gem env) is:
/opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/gems/1.8
If I go all the way into the nokogiri gem directory, I can successfully "require" it. But why can't I require it from anywhere else? I am misunderstanding something about the gem path.
Try to require 'rubygems' before requiring nokogiri. If there are no witches on your machine this could help.
Ruby prior to 1.9 didn't automatically do a require 'rubygems' for you. Post 1.9 it does. I always forget when I jump back to 1.8.7 to test something, especially in irb.
You can add require 'rubygems' to your ~/.irbrc file if you want. It won't hurt anything having it there.

Resources