gem install fails with "Could not find a valid gem 'yaml'" - ruby

I'm building a gem from a currently working ruby program. It's using jruby 1.7.12 and, among other things, does a "require 'yaml". For the gem, my Gemfile contains:
source 'https://rubygems.org'
gemspec
When I run
gem build program.gemspec
that works just fine, but when I run
gem install program-0.15.01.gem
it fails with
ERROR: Could not find a valid gem 'yaml' (>= 0) in any repository
ERROR: Possible alternatives: aml, cyaml, haml, maml, raml
Doesn't make any sense since the yaml module is part of the ruby 1.9.3 standard library.
I've upgraded to the latest rubygems (2.4.5).
What the heck am I missing?

yaml is part of Ruby. There is no yaml gem (see https://rubygems.org/search?query=yaml).
Therefore remove
s.add_runtime_dependency 'yaml'
from your gemspec and just add require 'yaml' to the file in which you want to use YAML.

Related

Optional runtime dependency for Ruby gem with executables

I'm writing a gem aipp which exposes a few executables, nothing fancy here:
#!/usr/bin/env ruby
require 'aipp'
AIPP::NOTAM::Executable.new(File.basename($0)).run
Parts of the gem optionally use database adapters (pg or ruby-mysql gem). But since these can be a pain in the butt when the gem is used on the cloud, I'd like to really make them optional and not require them as runtime dependency in the .gemspec.
So I require them conditionally at runtime:
require 'pg' if ENV['AIPP_POSTGRESQL_URL']
require 'mysql' if ENV['AIPP_MYSQL_URL']
Unfortunately, that doesn't work as expected. When either of the environment variables is set and the executable is used, the require fails – probably because there's no dependency declared.
I've tried an inline Gemfile on the executable like the following. Works in development (repo checkout), but not when the gem is intalled via Rubygems:
#!/usr/bin/env ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'aipp'
gem 'pg', '~> 1' if ENV['AIPP_POSTGRESQL_URL']
gem 'ruby-mysql', '~> 3' if ENV['AIPP_MYSQL_URL']
end
AIPP::NOTAM::Executable.new(File.basename($0)).run
What's the correct approach to require gems which are not listed as runtime dependency but installed additionally (via gem install or Gemfile)?
Maybe somebody knows of an existing gem which has already solved this problem somehow.
Thanks for your help!
I would use bundler groups for that.
gemfile do
source 'https://rubygems.org'
gem 'aipp'
group :database do
gem 'pg', '~> 1'
gem 'ruby-mysql'
end
end
There is more info about how to use them in the link.

Is the net/http gem not in the default library for Ruby?

I want to create a Gemfile.lock by typing 'bundle install' but my local machine can't find the gem net/http. I've tried typing 'bundle update net/http' & 'bundle --full-index' & 'gem install bundler' but I keep getting this error when I try 'bundle install' again:
Could not find gem 'net/http' in rubygems repository https://rubygems.org/ or installed locally. The source does not contain any versions of 'net/http'
my Gemfile resembles the following:
source "https://rubygems.org"
gem 'open-uri'
gem 'nokogiri'
gem 'net/http'
gem 'pry'
Other solutions to this problem suggest removing the line for gem net/http because net/http is part of the default library for Ruby...however when I do this everything loads fine, and I can create a Gemfile.lock upon typing 'bundle install' but when I run my code I get the following error:
Traceback (most recent call last):
run.rb:4:in `': uninitialized constant Net (NameError)
Did you mean? Set
The line of code this refers to is
response = Net::HTTP.get(url)
I'm running Ruby version 2.6.1
The name of the gem is net-http. It is one of the first hits when you google for "net/http".
However, in Ruby 2.6.1, net/http is still part of the standard library, not a gem. Net/http was only removed from the standard library in Ruby 3.0.
There are two different kinds of standard gems:
Default gems: These gems are part of Ruby and you can always require them directly. You cannot remove them. They are maintained by Ruby core.
Bundled gems: The behavior of bundled gems is similar to normal gems, but they get automatically installed when you install Ruby. They can be uninstalled and they are maintained outside of Ruby core.
Your problem is the wrong name gem ('net-http' instead of 'net/http', you can run gem search ^net to find out remote gems start by 'net').
If the gem is 'default', no need to declare it in Gemfile.
You can check standard default gems on: https://stdgems.org/

Ruby Gem not install dependency

I created a Ruby Gem and published it. I tried downloading it and I keep getting cannot require my dependent gem. The code is at https://github.com/wallerjake/toolshed and the gem in question is httparty. The error that I am getting is
~/.rvm/gems/ruby-2.0.0-p353#sullivan_cotter/bundler/gems/toolshed-46404c5af06d/lib/toolshed.rb:2:in `require': cannot load such file -- httparty (LoadError)
I have updated my https://github.com/wallerjake/toolshed/blob/master/toolshed.gemspec to use add_dependency instead but that doesn't seem to be helping. Could it be conflicting with other Gems?
The version on rubygems is still the old one with only development dependencies. Worked fine when I downloaded and built you gem from Github.
Yes sorry I solved this by adding them as a dependency not a development dependency.
spec.add_dependency "httparty"
spec.add_dependency "json"
spec.add_dependency "pivotal-tracker"
You have specified development dependency, but call the rake form binary, just put rake gem dependency into usual add_dependency, and remove ::gem call to:
bin/toolshed:
require 'rubygems'
require 'toolshed'
require 'rake'
One again question: for what do you need the binary?
The toolshed.gemspec seems correct. How do you call it? Remove the installed gem with gem uninstall toolshed including binary. Make sure that the bundle install, and then call to bundle exec bin/toolshed.rb is correct after patching the dependencies.
After that generate the gem with gem build toolshed.gemspec, and install the gem with gem install toolshed-0.0.4.gem. Make sure that binary works. Only then publish the gem.

How to install, require, and use a gem in ruby

I'm trying to use rake in my ruby script...(Ruby 1.8.6, JRuby 1.6.5)
Downloaded rake using gem install --remote rake, looks ok on install...
Fetching: rake-0.9.2.2.gem (100%)
Successfully installed rake-0.9.2.2
1 gem installed
I've got a simple ruby script which works fine, but when I import rake to using any of the following requires, it starts complaining....
require 'rake'
LoadError: no such file to load -- rake
or
require '/lib/rake'
LoadError: no such file to load -- lib/rake
After some searching, I found that adding require 'rubygems' just before rakefixes the issue....
require 'rubygems'
require 'rake'
Even though it's working, I've got some questions...
The gem spec on rake shows the require_path as lib, so why
doesn't require '/lib/rake' work? Am I misunderstanding the significance of require_path?
Why is it necessary to place require 'rubygems' before require
'rake'
Yes, you are misunderstanding the significance. The require_paths in the specification is an array of subdirectories of that gem's installation directory that should be searched for files belonging to the gem.
To find out where rake really is, try this:
$ gem which rake
You'll see that it is actually installed somewhere completely unrelated to /lib; on my system, it's under /var/lib/gems. Ruby gems, in general, live in their own directory structure; the only file in the standard Ruby include path ($:) is rubygems itself, which you used to have to explicitly require in order to make any actual gems visible. (Since Ruby 1.9, that has not been necessary.)
Gems are more complex than just libraries to load; you can have multiple versions of the same gem installed, and specify which one you want at a time, and do other things that wouldn't work if the gems were just dumped into the standard Ruby include path.
The require_path in the gemspec tells ruby where the files of this gem are located within the gem. It makes you able to type require 'rake', and ruby then knows it needs to look for /lib/rake within the gem installation folder.
In Ruby 1.8, rubygems (the mechanism responsible for making gems available to your app) is not loaded by default, and the default ruby isn't aware of any gem on your system. You need to load rubygems before being able to require any other gem. This is not the case anymore with Ruby 1.9.

Where is ruby gem specifying the ruby version?

Building a simple gem, using echoe, just doing something like
rake manifest
rake gem
gem install
There's nothing in the generated files (or anywhere in the project, for that matter) where I'm referencing or specifying a ruby version, but trying to install my gem gives me a "requires ruby >= 1.9" message.
So, where is the ruby version being derived? (the generated gemspec does contain the lines
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if ...
s.rubygems_version = %q{1.3.5}
if that's at all relevant - only references I could find with any version information at all)
Check in gem itself. Looking at /usr/bin/gem on my machine gives me this on line 12.
required_version = Gem::Requirement.new ">= 1.8.6"

Resources