Can't find rubygem when it is clearly there? - ruby

I've just made a little gem and pushed it to Rubygems.org:
https://rubygems.org/gems/fragrant_wind
However, I can't seem to download it again, any ideas?
$ gem install fragrant_wind
ERROR: Could not find a valid gem 'fragrant_wind' (>= 0) in any repositor

Try fixing your gem name. Choose either underscores or hyphens, but don't mix them.
For example you have:
# .gemspec
s.name = 'fragrant-wind'
Better is:
# fragrant_wind.gemspec
s.name = 'fragrant_wind'
Edit 1: it works for me to download then install, but it doesn't work via RubyGems.
$ gem install fragrant_wind-0.0.2.gem
Successfully installed fragrant_wind-0.0.2
1 gem installed
Installing ri documentation for fragrant_wind-0.0.2...
Building YARD (yri) index for fragrant_wind-0.0.2...
Installing RDoc documentation for fragrant_wind-0.0.2...
$ irb
> require 'fragrant_wind'
=> true
> FragrantWind.new.generate
=> "billowing waterfall"
Edit 2: it works via bundler too.(Thanks to #Bitterzoet for this)
# Gemfile
source 'https://rubygems.org'
gem "fragrant_wind"
$ bundle install
Fetching gem metadata from https://rubygems.org/..
Installing fragrant_wind (0.0.2)
Using bundler (1.2.3)
Your bundle is complete! ...
Edit 3: it works now as expected (Monday 10 a.m. PST). My guess is Rubygems was having a delay or glitch due to its own update last night.

Related

Ruby - Cannot use locally installed gem

I've written a simple PasswordGenerator gem that I have at ~/workspace/gems/password_generator and have an app at ~/workspace/rubysamples/app where I want to use it. I have a Gemfile, the content of it is this:
gem 'password_generator', path: '~/workspace/gems/password_generator'
I installed it locally, like this:
bundle install --local
Resolving dependencies...
Using bundler 1.16.5
Using password_generator 0.1.0 from source at `~/workspace/gems/password_generator`
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
It looks like it's installed locally:
bundle info password_generator
* password_generator (0.1.0)
Summary: Simple password generator
Homepage: https://github.com/jedrekdomanski/password_generator
Path: /home/jedrek/workspace/gems/password_generator
When I try to use it
~/workspace/rubysamples/app/password_reset.rb
PasswordGenerator.generate
I get an error
uninitialized constant PasswordGenerator (NameError)
What am I doing wrong? Am I missing anything?
Here's my gem repo: https://github.com/jedrekdomanski/password_generator
I also tried pointing to my repo and branch in the Gemfile
gem 'password_generator', git: 'git#github.com:jedrekdomanski/password_generator.git', branch: 'master'
but I get the same error message uninitialized constant PasswordGenerator (NameError)
There are potentially two issues. The first is how you are starting Ruby and the second is how you are requiring your module.
First, if you are starting Ruby by running ruby password_reset.rb then you are ignoring the Gemfile. The Gemfile is only used when you're using bundler, so you want to make sure you are starting Ruby by running bundle exec ruby password_reset.rb. This causes bundler to read your Gemfile and execute Ruby in that context.
Second, you're not properly including your module in your Ruby file. Just because you've added the gem to your Gemfile and started Ruby using bundler doesn't mean that the Ruby process knows you intend to use that gem's module; it just makes the module available for use. You might wonder, "Why don't I have to do that in Rails?" Because Rails does that for you automatically via config/application.rb.
Given these two issues, the correct way to accomplish your goal is to configure your app as follows:
First, create your Gemfile:
# Gemfile
gem 'password_generator', path: '~/workspace/gems/password_generator'
Second, create your password_reset.rb file:
# password_reset.rb
# Manually require any libraries that this app will use, even if defined in Gemfile
require 'password_generator'
# Call `puts` so something is printed to the console when this app runs
puts PasswordGenerator.generate
Third, run bundle install to ensure your Gemfile is properly formatted and to generate your Gemfile.lock:
⇒ bundle install
Using bundler 1.16.5
Using password_generator 0.1.0 from source at `../../gems/password_generator`
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Fourth, run bundle exec ruby password_reset.rb and observe the output:
⇒ bundle exec ruby password_reset.rb
kpiDfyTxtdAsKmYuZqmK
Everything works because:
Ruby is started with Bundler
Bundler reads your Gemfile and makes the gems available to Ruby
Your app requires the module from the gem before attempting to use the module

Why can't Ruby's bundle install find the gem I created?

I'm trying to use a Ruby gem that I created (Called "mygem"). But my rails application cannot find it. Can someone please advise why and how to fix it?
Step #1: Verify that mygem exists in my current directory:
$ pwd
/myDir
$ ls mygem-1.7.3.gem
-rw-r--r-- 1 myuser wheel 27136 Aug 6 11:41 mygem-1.7.3.gem
Step #2: Make sure the Gemfile says to pickup my gem from the right place:
$ grep mygem Gemfile
gem "mygem", :path => "/myDir"
Step #3: Do bundle install.
But when I do that, it says it can't find it:
$ bundle install
Fetching gem metadata from http://rubygems.org/........
Fetching gem metadata from http://gems.myserver.com/...
Fetching additional metadata from http://rubygems.org/....
Fetching additional metadata from http://gems.myserver.com/..
Resolving dependencies...
Could not find gem 'mygem (>= 0) ruby' in source at /myDir.
Source does not contain any versions of 'mygem (>= 0) ruby'
Try specifying the version number
gem "mygem", '1.7.3', :path => "/myDir"
Credit goes to this answer

How to update ruby gem dependency? Must I rebuild it? How?

I have a gem already installed locally, and I wanna add a new dependency into it. I worked with below steps:
Open the gem installation folder as below:
/usr/local/rvm/gems/ruby-2.1.0/gems/nesta-0.10.0
Add new dependency below into gemspec file (nesta.gemspec)
s.add_dependency('stacktracer', '>= 0.0.1')
Append gem 'stacktracer' into Gemfile
Add require "stacktracer" into Rakefile
Run command bundle update nesta
Run command bundle install
Check Gemfile.lock, I already found the new dependency like below:
I have next dependencies:
DEPENDENCIES
debugger
mr-sparkle (>= 0.0.2)
nesta!
rack-test (= 0.6.1)
rspec (~> 2.14.0)
**stacktracer**
test-unit (= 1.2.3)
webrat (~> 0.7.3)
But when I met two problems here:
run command gem dependency nesta, I did not find stacktracer dependency list.
I added require "stacktracer" into app.rb, then run the application, it failed due to below error:
/usr/local/rvm/gems/ruby-2.1.0/gems/nesta-0.10.0/lib/nesta/app.rb:4:in `require': cannot load such file -- stacktracer (LoadError)
Could any one help me to tackle the problems? Is there any approach that could manage to update gem dependency successfully?
You are not supposed to edit the unpacked gems under .../rvm/gems/ruby-2.1.0/gems. If you need to update the alien gem, you are to follow this approach:
Fork the gem source at github
Clone the repo to your local drive
Update whatever you want + update version
Set the dependency in your Gemfile to either use unpacked version or your git:
gem 'nesta', :path => '.../nesta.git'
gem 'nesta', :git => 'git://...'
Don’t forget to eliminate the original gem presence over your system. Hope it helps.

How to tell the bundle to use specific version of hpricot [duplicate]

Upgraded to rails 3, and using Bundler for gems, in a mixed platform development group. I am on Windows. When I run Bundle Install it completes succesfully but will not install hpricot. The hpricot line is:
gem "hpricot", "0.8.3", :platform => :mswin
also tried
gem "hpricot", :platform => :mswin
Both complete fine but when I try to do a "bundle show hpricot" I get:
Could not find gem 'hpricot' in the current bundle.
If I do a run a rails console and try "require 'hpricot'" I get:
LoadError: no such file to load -- hpricot
I have manually installed hpricot as well, and still get the above error. This worked fine before moving to rails 3.
Try this in console and then do bundle install, it will work:
gem install hpricot --platform=mswin32
It may be that you are using a version of ruby that was built with MinGW in which case your platform will be 'mingw' rather than 'mswin'. Try changing your Gemfile line to the following:
gem "hpricot", "0.8.3", :platform => :mingw
And if you have other developers that are using the current setup and don't wan to break it for them, change it to this:
gem "hpricot", "0.8.3", :platforms => [:mswin, :mingw]

Ruby 1.9 - no such file to load 'win32/open3'

I'm running ruby 1.9.2 on Windows and am trying to port code that worked in Ruby 1.8. The code uses Open4.popen4 which previously worked fine. With 1.9.2 I have done the following:
Installed POpen4 via gem install POpen4
Required POpen4 via require 'popen4'
Attempted to use POpen4 like:
Open4.popen4("cmd") {|io_in,io_out,io_er| ... }
When I do, I get the error:
no such file to load -- win32/open3
If I try and install win32-open3 (gem install win32-open3) I get the error:
win32-open3 requires Ruby version < 1.9.0
Does anyone know how I get around this problem?
Haven't used it, but this might work: https://github.com/matschaffer/win32-open3-19
Adding
gem "win32-open3-19", :platforms => :mingw, :git => "github.com/matschaffer/win32-open3-19.git"
to my Gemfile didn't exactly work.
Here are the steps that solved this for me:
Add this to the Gemfile -> gem 'win32-open3-19', :platforms => :mingw
Run bundle to install win32-open3-19
That was it. For me the git location was unncessary and didn't work.

Resources