Cannot find octokit gem when running ruby, but irb can find it - ruby

I ran rvm implode to get a fresh install, then reinstalled RVM and ruby. Then I installed the octokit gem which I want to run.
When I run require 'octokit' in irb everything works, but when I try it from the command line, like such:
ruby file.rb where file.rb is:
require 'octokit.rb'
require 'csv.rb'
CSV.open("node_attributes.csv", "wb") do |csv|
csv << [Octokit.user "dbussink"]
csv << [Octokit.user "sferik"]
end
I get:
1:in require: no such file to load -- /octokit (LoadError)
which ruby yields /usr/bin/ruby, which irb yields /usr/bin/irb, but which octokit yields octokit not found.
Further, rvm list yields:
rvm rubies
=* ruby-2.0.0-p247 [ x86_64 ]
# => - current
# =* - current && default
# * - default
And, gem list octokit yields:
*** LOCAL GEMS ***
octokit (1.25.0, 1.4.0)
How can I make sure that I can find octokit when running ruby from the command line? I've tried changing the path, using an explicit path in the require command, etc., but nothing seems to work!

You need to import require 'octokit' not require 'octokit.rb'. I see why you'd require the latter (name of the project on GitHub).

Related

Ruby programmatically installing a gem

If I install a gem programmatically using Backticks in script with gem install gem_name as:
if not_gem_installed
`gem install my_gem`
end
require 'my_gem'
and then require the gem, a LoadError comes up. Any way to fix this?
I've tried installation of gems with backticks and found that I have the same issues as mentioned in question. I see that it's not the optimal choice for this job.
I would suggest to use Gem for search of gems and installation.
require 'rubygems/commands/install_command'
def find_or_install(gem_name)
Gem::Specification::find_by_name(gem_name)
rescue Gem::LoadError
install_gem(gem_name)
end
def install_gem(gem_name)
cmd = Gem::Commands::InstallCommand.new
cmd.handle_options [gem_name]
cmd.execute
rescue Gem::SystemExitException => e
puts "FAILURE: #{e.exit_code}"
end
https://gist.github.com/adamjmurray/3154437 - Gist with good examples
Since you are not using RubyGems, you need to require specific file from your gem's path. RubyGems overrides the require method so that it looks for paths file to include from gem paths in your Gemfile. So, make sure you specify correct path to your gem's main .rb file.
if not_gem_installed
`gem install my_gem`
end
require 'path/to/my_gem'
You can check the paths ruby will look into by printing the value of $: in your file/irb or by running gem environment command in your command line.
uzbekjon#mini:~$ gem environment
RubyGems Environment:
- RUBYGEMS VERSION: 2.5.1
- RUBY VERSION: 2.3.0 (2015-12-25 patchlevel 0) [x86_64-darwin15]
- INSTALLATION DIRECTORY: /Users/uzbekjon/.rvm/gems/ruby-2.3.0
- USER INSTALLATION DIRECTORY: /Users/uzbekjon/.gem/ruby/2.3.0
- RUBY EXECUTABLE: /Users/uzbekjon/.rvm/rubies/ruby-2.3.0/bin/ruby
- EXECUTABLE DIRECTORY: /Users/uzbekjon/.rvm/gems/ruby-2.3.0/bin
- SPEC CACHE DIRECTORY: /Users/uzbekjon/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Users/uzbekjon/.rvm/rubies/ruby-2.3.0/etc
- GEM PATHS:
- /Users/uzbekjon/.rvm/gems/ruby-2.3.0
- /Users/uzbekjon/.rvm/gems/ruby-2.3.0#global
- GEM CONFIGURATION:
...
=== OR with $: ===
uzbekjon#mini:~$ irb
2.3.0 :001 > $:
=> ["/Users/uzbekjon/.rvm/gems/ruby-2.3.0#global/gems/did_you_mean-1.0.0/lib", "/Users/uzbekjon/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0", "..."]
Alternatively
You can require simply use bundler in your script.
if not_gem_installed
`gem install my_gem`
end
require 'rubygems' # <= This line here
require 'path/to/my_gem'

How do I set correctly use RVM gemsets with the guard-shell plugin?

I am trying to run a Ruby script via guard-shell. This script requires the redcarpet gem. When the script runs, I get a "cannot load such file" error, indicating that the gemset I'm expecting will be used is in fact not being used.
Here are some relevant parameters:
In the Guard app folder (where the Guardfile is):
> ls -a
. .ruby-gemset Gemfile Guardfile
.. .ruby-version Gemfile.lock tmp1.txt
> cat .ruby-*
rails4
2.1.2
The gem seems to be there:
> gem which redcarpet
/Users/sameer/.rvm/gems/ruby-2.1.2#rails4/extensions/x86_64-darwin-13/2.1.0-static/redcarpet-3.1.2/redcarpet.bundle
My Guardfile has watch(/(.*)/) {|m| `rvm list; rvm gemset list; gem which redcarpet` } which outputs:
ERROR: Can't find ruby library file or shared library redcarpet
ERROR: Can't find ruby library file or shared library redcarpet
rvm rubies
ruby-2.0.0-p353 [ x86_64 ]
ruby-2.1.0-preview1 [ x86_64 ]
ruby-2.1.1 [ x86_64 ]
=* ruby-2.1.2 [ x86_64 ]
# => - current
# =* - current && default
# * - default
gemsets for ruby-2.1.2 (found in /Users/sameer/.rvm/gems/ruby-2.1.2)
(default)
global
=> rails4
So what am I missing?
I get the same error you do, e.g.:
./my_prog.rb:1:in require': cannot load such file -- nokogiri
(LoadError) from ./my_prog.rb:1:in'
But if I put the gem that the ruby program requires in the Gemfile, then the ruby program executes without error. If I comment out the line in the Gemfile, then once again the error occurs. The gem is installed either way, but if it's not listed in the Gemfile, then guard-shell produces that error.
My Guardfile:
guard :shell do
watch /.*/ do |m|
puts 'hello'
`ruby ./my_prog.rb`
end
end
My Gemfile:
source 'https://rubygems.org'
group :development do
gem 'guard', '2.6.1'
gem 'guard-shell', '0.6.1'
gem 'nokogiri', '1.6.2.1'
end
my_prog.rb:
require 'nokogiri'
puts 'goodbye'
Somewhat annoyingly, this is my output in the guard shell:
hellouard(main)>
goodbye
[1] guard(main)>

ruby adds no path to gems

I use:
rvm 1.25.19 (stable)
ruby 1.9.3p545
I install gem rake command
gem install rake
I understand that $LOAD_PATH should contain the paths:
~/.rvm/rubies/ruby1.9/gems/rake/lib
~/.rvm/rubies/ruby1.9/gems/rake/bin
I execute command:
ruby -e 'puts $LOAD_PATH'
output:
/home/slip/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/site_ruby/1.9.1
/home/slip/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/site_ruby/1.9.1/x86_64-linux
/home/slip/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/site_ruby
/home/slip/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/vendor_ruby/1.9.1
/home/slip/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/vendor_ruby/1.9.1/x86_64-linux
/home/slip/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/vendor_ruby
/home/slip/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1
/home/slip/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/x86_64-linux
why path to gem 'rake' don't add?
Installing a gem in Ruby doesn’t automatically add that gem’s files to the load path. When you require a file, first the existing load path is searched and if no matching file is found then Rubygems searches the installed gems for the file. If such a gem is found, then it is activated, which is when the gem’s lib directory is added to the load path.
Here’s an example using the Haml gem:
puts "Before:"
puts $LOAD_PATH
puts
require 'haml'
puts "After:"
puts $LOAD_PATH
This produces:
Before:
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/site_ruby/1.9.1
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.8.0
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/site_ruby
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/vendor_ruby/1.9.1
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.8.0
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/vendor_ruby
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/x86_64-darwin10.8.0
After:
/Users/matt/.rvm/gems/ruby-1.9.3-p448/gems/tilt-1.4.1/lib
/Users/matt/.rvm/gems/ruby-1.9.3-p448/gems/haml-4.0.5/lib
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/site_ruby/1.9.1
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.8.0
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/site_ruby
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/vendor_ruby/1.9.1
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.8.0
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/vendor_ruby
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1
/Users/matt/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/x86_64-darwin10.8.0
See how the lib directories for Tilt (which is a dependency of Haml) and Haml have been added to the start of the load path.
In the case of some gems, such as Rake, this is complicated by the fact they are part of the standard library and so are already on the load path. If you changed the example above to use require 'rake' then you wouldn’t see any change in the load path. In this situation you can specify you want to use the gem version with the gem method. This activates the gem in question, placing its lib directory at the start of the load path so when you later call require you will load the correct version.
For example, this script
require 'rake'
puts Rake::VERSION
produces 0.9.2.2 with Ruby 1.9.3, as that is the version of Rake included in the standard library. However this script:
gem 'rake'
require 'rake'
puts Rake::VERSION
produces 10.1.1, since that is the current highest version of the Rake gem installed on my system. You can also specify a particular gem version by providing a second argument to gem (this is the same syntax as used in Gemfiles). Note that the gem method doesn’t require any files, you have to do that as a separate step.

Can't get awesome_print gem to work

awesome_print looks like a pretty nice gem, so I wanted to try it out.
I went to one of my projects and did:
gem install awesome_print
and it says one gem installed, documentation installed, etc.
Then, while I am in that project, I went to my Rails console to try it out, but when I did a require "awesome_print" as their help file says, I get a "cannot load such file".
Has anyone got this to work?
gem install will put the gem code on your computer, but unless the gem's source code files are on your load path, require won't be able to find them. bundle exec looks at the nearest Gemfile.lock and adds the source code for all the gems listed there to your load path. Rails initialization includes getting Bundler to do this for you.
One solution is to add awesome_print to your Gemfile. However, this will cause your application to have awesome_print as a dependency. Alternatively you can manually add the awesome_print library to your load path after starting up the Rails console and then requiring it:
$ rails c
> $LOAD_PATH << path/to/awesome_print-x.x.x/lib
> require 'awesome_print'
> ap {foo: {bar: {baz: :qux}}}
If you're using RVM, the path is likely to be something like:
~/.rvm/rubies/ruby-x.x.x-pxxx#your_gemset_name/gems/awesome_print-x.x.x/lib
Add it to your Gemfile like this:
gem 'awesome_print', :require => 'ap'
I add it to the development group, since that's the only time I need it. The gem doesn't have any other gem dependencies, so I routinely add it to my Gemfile.
Also, add these two lines to your ~/.irbrc file to set ap to be your default pager:
require "awesome_print"
AwesomePrint.irb!
Note that if you use this, however, any projects where awesome_print is not installed in its Gemfile will raise this error when you run rails c:
cannot load such file -- awesome_print
Depending on whatever else you may have in your ~/.irbrc file, this can cause other side effects, such as messing up your prompt. To avoid these, simply add the two lines to the very end of that file.
install it :
$ gem install awesome_print
include it in you GemFile, if you want :
gem 'awesome_print', :require => 'ap'
add this line to the file ~/.irbrc :
require 'awesome_print'
AwesomePrint.irb!
restart your shell!
just a note: I did this and it didnt work right away, probably need to restart the computer... or I just needed to close all shell tabs and open the terminal again!
Install the gem on your machine
gem install awesome_print
Get the path to which it has installed
gem which awesome_print
Add the following configuration to your ~/.irbrc and ~/.pryrc. This will load Awesome Print whenever you fire an IRB or a pry session.
*Remember $LOAD_PATH will hold whatever you got from typing gem which awesome_print
# ~/.irbc and ~/.pryrc
$LOAD_PATH << "~/.asdf/installs/ruby/2.6.3/lib/ruby/gems/2.6.0/gems/awesome_print-1.8.0/lib/"
require "awesome_print"
AwesomePrint.irb!
If you are looking to install it without having it in your Gemfile, this is how to do it:
$ gem install awesome_print
I was running into an issue where it was installing successfully but it not in the right directory.
In that case just put this in your .bashrc, this will set the load path:
export PATH="/home/user/.gem/ruby/2.3.0/bin:$PATH"
PATH="`ruby -e 'puts Gem.user_dir'`/bin:$PATH"
replace 2.3.0 with the version of ruby you are working with.
replace user with your username or if you are using vagrant then replace with vagrant
reload your .bashrc or exit the Terminal to reload changes, then install the gem again.
In my case, I struggled with PATHs and such, while missing something obvious!
# which ruby
/usr/bin/ruby
# ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin17]
# locate bin/ruby
/usr/bin/ruby
/usr/local/Cellar/ruby/2.7.2/bin/ruby
/usr/local/opt/ruby/bin/ruby
# /usr/local/opt/ruby/bin/ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin17]
#
Aha! Version crud. I was running an old ruby. Thanks, Apple!
# sudo mv /usr/bin/ruby /usr/bin/ruby_2.3.7
# sudo ln /usr/local/opt/ruby/bin/ruby /usr/bin/ruby
Solved the problem!
There is probably something I could have told brew to do to fix things, but I was impatient. :-)

How to reference a local gem from a ruby script?

I need to reference a local gem from a plain ruby script, without installing the gem. On the trail of How to refer a local gem in ruby?, i tried creating a Gemfile with the following setup:
%w(
custom_gem
another_custom_gem
).each do |dependency|
gem dependency, :path => File.expand_path("../../#{dependency}", __FILE__)
end
and the script looks like this:
require 'custom_gem'
CustomGem::Do.something
When I execute this with:
bundle exec ruby script.rb
I get:
script.rb:1:in `require': cannot load such file -- custom_gem (LoadError) from script.rb:1:in `<main>'
If I leave out the require 'custom_gem' , I get:
script.rb:3:in `<main>': uninitialized constant CustomGem (NameError)
I even tried without bundler, and just writing gem ... :path =>̣ ... in the script itself, but without results. Is there any other way of referencing custom gems from ruby scripts, without installing the gems locally?
Make sure that your gem name as same as in Gemfile (e.g. custom_gem)
# Gemfile
source "https://rubygems.org"
gem "custom_gem", path: "/home/username/path/to/custom_gem"
Don't forget to actually install this gem using bundler
bundle install
After that, the script should be ready to use by bundle exec ruby script.rb
# script.rb
require 'custom_gem'
CustomGem::Do.something
Without using a Gemfile, you can install a local version of a gem by running bundle exec rake install in the gem's root directory and then you can reference it just like any other installed gem.

Resources