Ruby: require downloaded gems - ruby

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.

Related

Ruby script cannot load a gem installed via bundler

I am trying to include the ruby-mysql gem in my ruby script. I have installed the gem using bundler but when I run bundle exec ./mysql_connector, I receive the error ./mysql_connector:4:in ``require': cannot load such file -- ruby-mysql (LoadError). Can you please help me troubleshoot what the problem is?
What I did
Installed rails in my home directory.
I do not have root access to the server so I have installed rails in my local directory by following the instructions here:
http://www.r-bloggers.com/installing-ruby-on-linux-as-a-user-other-than-root/
Created a directory for my application.
My application resides in my home directory in a folder called connector. It has a Gemfile that looks like this:
source 'https://rubygems.org'
gem 'ruby-mysql'
Call bundle install.
Using ruby-mysql 2.9.14
Using bundler 1.11.2
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Bundled gems are installed into ./vendor/bundle.
Add dependencies to my script. My script is in connector/mysql_connector and it reads:
#!/home/dcox/bin/ruby
require 'rubygems'
require 'bundler/setup'
require 'ruby-mysql'
Make script executable. I saw that you need to run bundle exec using an executable file, so I followed the instructions here to make my script executable: http://commandercoriander.net/blog/2013/02/16/making-a-ruby-script-executable/
Run the script. I execute using bundle exec mysql_connector and see:
/home/dcox/bin/mysql_connector:4:in `require': cannot load such file -- ruby-mysql (LoadError)
from /home/dcox/bin/mysql_connector:4:in `<main>'
Is it the $LOAD_PATH? After searching around for answers, I discovered a lot of SO answers as well as a blog post (https://codedecoder.wordpress.com/2013/09/23/require-and-load-in-ruby-loaderror-cannot-load-such-file/) that seem to suggest the problem is that the gem is not installed in a directory on the $LOAD_PATH. Here is what I see when I run $LOAD_PATH from IRB:
irb(main):002:0> $LOAD_PATH
=> ["/home/dcox/lib/ruby/site_ruby/2.1.0",
"/home/dcox/lib/ruby/site_ruby/2.1.0/x86_64-linux",
"/home/dcox/lib/ruby/site_ruby", "/home/dcox/lib/ruby/vendor_ruby/2.1.0",
"/home/dcox/lib/ruby/vendor_ruby/2.1.0/x86_64-linux",
"/home/dcox/lib/ruby/vendor_ruby", "/home/dcox/lib/ruby/2.1.0",
"/home/dcox/lib/ruby/2.1.0/x86_64-linux"]
Next I checked to see the location of ruby-mysql:
dcox#analytics1:~/connector$ bundle show ruby-mysql
/data/home/dcox/connector/vendor/bundle/ruby/2.1.0/gems/ruby-mysql-2.9.14
Obviously my connector/vendor/bundle path is not on the $LOAD_PATH. I could add it, but I have a feeling I am missing something simple here because bundler is supposed to work as long as you follow the instructions, right?
Any advice or help is very much appreciated! Thanks!!
If you just want to require this specific gem, require 'mysql' should work (e.g., https://github.com/tmtm/ruby-mysql/blob/master/test/test_mysql.rb#L10).
Your file should call Bundler.setup http://bundler.io/bundler_setup.html
Better yet, if you instead call Bundler.require(:default) it will setup and require all the gems in your Gemfile for you.

Rake Cannot Load Such File

I have installed the state_machine gem. "gem list" reports it installed locally. I am actually using the gem successfully.
$ ruby peer_state_machine_test.rb
runs great. However when I try to rake, I hit a failure to load that gem.
peer_state_machine.rb:1:in `require': cannot load such file state_machine (LoadError)
The line of the file in question is:
require 'state_machine'
I'm confused as to how there is any difference in the way ruby loads this file and rake loads it. There are many other test files which are running without any problems. This is on OS X if it matters.
I was writing a gem and forgot to add the state_mahine gem as a development dependency to my gemspec. Adding
spec.add_development_dependency "state_machine", "~> 1.2.0"
resolved the problem.

Missing gem watir?

ahmet#Ubuntu:~/test$ ruby hello.rb
/home/ahmet/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': no such file to load -- watir (LoadError)
from /home/ahmet/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from hello.rb:2:in `<main>'
I have required rubygems in my .rb but still get this error
require 'rubygems'
require 'watir'
According to Watir Readme you have to install and require firewatir. Also you may want to install watir-webdriver instead.
I encountered a similar problem with a different gem a while back on Ubuntu using RVM. I know the gem was installed, because I had just installed it. I did the following:
rvm get latest
rvm reload
gem update --system
Then for good measure I reinstalled the gem, and everything worked. Hope this helps you as well.
Read this: https://github.com/zeljkofilipin/watirbook/blob/master/installation/ubuntu.md
The require should be either 'firewatir' or 'watir-webdriver'
require 'firewatir'
or
require 'watir-webdriver'

require 'nokogiri' issue with Ruby 1.9.2

I have made a Ruby project (not rails project) in Netbeans, and in main.rb file i have been requiring nokogiri but i am getting the following error.
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- nokogiri (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from /home/nadeem/rails_project/RubyApplication1/lib/main.rb:2:in `<main>'
I have installed nokogiri 1.5.0 and using ruby 1.9.2
Any idea?
Using bundler to manage your gem dependencies is probably a good idea because you can check if things are set up properly using bundle check if you've declared and successfully installed them.
The bundler setup routine can import many gems in the proper order, accounting for dependencies. Otherwise you will need to have at least this:
require 'rubygems'
gem 'nokogiri'
require 'nokogiri'
Make sure that the gems are installed with the same version of Ruby you're trying to run. It's easy to get mixed up when you're using rvm and have inadvertently added the gems to a different version.
Refer to this solution hope this will be helpful, Check your net bean's project properties and try to update path from there.
Require Nokogiri? No such file to load

no such file to load - require wordnet fails - troubleshoot gem installation

my ruby environment cant see an installed gem, at least the require fails
It's about the gem wordnet http://deveiate.org/projects/Ruby-WordNet I start an example script ( as user of course )
./meronymTree.rb
./meronymTree.rb:8:in `require': no such file to load -- wordnet (LoadError)
from ./meronymTree.rb:8
In some example scripts it'S called "WordNet" and in some it's lowercase, the error is same.
here's the scripts first lines
#!/usr/bin/ruby -w
$LOAD_PATH.unshift "lib"
require 'wordnet'
Check if gem is installed, as user root
gem list |grep word
wordnet (0.0.5)
root#auedv18:/var/lib/gems/1.8/gems/wordnet-0.0.5# ls
ChangeLog convertdb.rb examples lib LICENSE rake Rakefile Rakefile.local README spec utils.rb
Other gems are fine.
I am on current ubuntu and got ruby1.8 and rubygems-1.8.6 on it, (if that makes a difference).
hm and i downloaded the wordnet-0.0.5.gem file from above site and did
gem install wordnet-0.0.5.gem
Try this:
require 'rubygems'
require 'wordnet'

Resources