fixing a failed ruby gem install - ruby

I am installing the ruby gem mysql-dbd on a new system which is running ruby 2.5. The problem is that it gets a syntax error because at 2.4 ruby combined Integer and Fixed num types.
The failed install leaves the unpacked gem package on disk so I was able to examine the entrails and the fix appears to be trivial (as in insert a '#' in a statement to remove the now redundant reference to FixNum).
My question is having fixed the source how do I go about building and installing the gem? Not being familiar with rake.
BTW the gem has long been "unsupported".

I can't get the gem source repository to load, but you've got at least two options:
Locally, you might be able to build it. Try running rake build from the root folder of the gem, and look in the pkg folder for the built gem. You should be able to then gem install pkg/<gem name>.gem in that folder
If you need to share it with others, push the code up on github. Make your change, and if you're using bundler in the codebase that's using the gem, update your Gemfile to point to your source.

You could try something like this:
# install_dbd_mysql.rb
Fixnum = Integer
require 'rubygems/commands/install_command'
install = Gem::Commands::InstallCommand.new
install.handle_options ['dbd-mysql']
install.execute
Run:
$ ruby install_dbd_mysql.rb

Related

Can't install gem using Bundler's Rakefile install task when developing a custom gem

I'm developing a couple of private gems and I think I don't understand correctly the PATH/GEM_PATH and/or Bundler/RVM installation flow, would love if someone could chip in.
I have a repository with two gems (A & B for simplicity sake). I've developed the gems using the scaffolding + following the guidelines provided by this bundler tutorial.
Thanks to the Bundler project I have a few Rakefile tasks like rake build, rake install, rake install:local and rake release. Because of the private nature of these gems I can't release them to RubyGems (and we haven't looked into hosting our rubygems).
My machines are using RVM to manage ruby versions and Bundler version 1.15.1
What I want to do: Assuming a new machine/developer trying out the project, ideally we would cd into each of the subfolders (currently 2, gem A and gem B), run rake install and after that we should have the gems available system wide for the current user.
What is happening: The gems are built and work properly, but they are only available inside the subfolder of each gem i.e. gem A is only available inside the subfolder A and gem B is only available inside subfolder B.
What I've tried: So, after rake build/install/install:local a new .gem file is generated under pkg. I've tried to manually install the "compiled" file using gem install pkg/A.gem, gem install --local pkg/A.gem and gem install --local --user-install pkg/A.gem without success. (there are plenty of SO questions/answers about this)
I believe this has something to do with the PATH variables, but like I said before I don't fully understand the way they are managed. I get the following results from these commands:
# Our gem
> gem which A
/home/ubuntu/.rvm/gems/ruby-2.4.0/gems/A-0.1.8/lib/A.rb
# Pry, available globally
> gem which pry
/home/ubuntu/.rvm/gems/ruby-2.4.0/gems/pry-0.11.1/lib/pry.rb
I've been lost and frustrated for far too long now, any help is appreciated. Also open to hear suggestions of better private gem installation flows :)
Yes, it has something to do with your PATH variables. Your installation seems to be good.
I advise you to first affirm your gems installation path with:
echo $GEM_HOME
The double check your PATH to ensure its present and also confirm that the GEM home is also where the gem got installed into from the rake install
echo $PATH
If not, put it in your path and you should be fine with something like this:
echo PATH=$PATH:$GEM_HOME >> ~/.bashrc
source ~/.bashrc
Build your gem as per that guide you linked. You should end up with a gem file. Distribute this as you see fit (I use rsync/crontab to download newer gem versions but anything goes). User can install the gem as follows:
gem install --user-install /path/to/your/file.gem
This will install the gem in the user's ~/.gem/ruby/<version>/gems/<your-gem-name> directory.
Tried it with an empty gem (foodie, as in that example guide) and it works fine. But if you don't specify the --user-install parameter it will try to install in the system ruby dir (/usr/lib/ruby/gems...)

How to contribute to a Ruby Gem

I am trying to contribute to a Ruby gem and I don't understand how to test a local gem without using a globally installed gem.
The gem I want to contribute to is a command line interface gem. I clone the gem into a directory then cd into that directory. However, when I run commands in the terminal when I'm in the cloned project directory it still uses the global gem. I've even run
gem uninstall gemname
then while inside the newly cloned gem directory I redo
gem install gemname.
No matter what changes I make to the gem, I can't see the results or what my contributions are doing because it's always running the global gem.
When I do try to type a command line command that is supposed to interact with the gem while in the cloned gem directory I get:
-bash: ~/.gem/ruby/2.1.0/bin/githubrepo: No such file or directory
I've done a ton of research but I'm just not getting it. Help?
gem install gemname will look for a .gem file in the current directory. If not found it will look for it on the web.
gem install --local /path/to/your/gemname.gem will allow you to target a particular directory. You may need to gem build gemname.gemspec first, so it has your changes.
Instead of doing this, I would write tests in the gem directory itself. It's likely that when running code in there, you can simply require 'gemname' in Ruby to get the gem functionality.
If it's a well-written gem, it should have tests already. They will most likely be in a directory called test or spec. Have a look at these tests and try to carry on in that style to test your changes. This will make your code changes far far more likely to be accepted as a pull request.

How to safely fix "invalid date format in specification" for webrobots-0.0.10.gemspec

For every gem execution I am receiving the following error:
Invalid gemspec in [...rvm/gems/ruby-1.9.2-p136/specifications/webrobots-0.0.10.gemspec]: invalid date format in specification: "2011-07-01 00:00:00.000000000Z"
I am not the webrobots maintainer and I can't uninstall that gem.
There is any safe action to fix this problem on a production server?
These days with Github and Bundler you don't have to be a maintainer in order to fix issues in the gems you're using. Just fork the project on Github, fix whatever issue is there, and them simply point Bundler at your fork. If it is a handy extension to the gem rather than just a hack for yourself you might also consider making a pull request to the original maintainer to roll your changes into the official gem.
Having said all of that, the issue that might cause this problem doesn't seem to be present in the gemspec of the 0.0.10 version of webrobots. This issue can occur when the date in the gemspec is in the format that you have in your error e.g.:
"2011-07-01 00:00:00.000000000Z"
Looking at a snippet of gemspec though we have:
s.authors = [%q{Akinori MUSHA}]
s.date = %q{2011-07-01}
s.description = %q{This library helps write robots.txt compliant web robots in Ruby.
So that seems to be correct. Another issue that has been known to cause this problem is a YAML parser issue, to fix you need to force the use of a particular YAML parser (psych). You need to define the following somewhere:
require 'yaml'
YAML::ENGINE.yamler = 'syck'
Lastly you might want to clean your local installation and then do a redeploy which may sort you out. Once again if you're using Bundler something like this:
gem update --system
rvm gemset empty mygemset
gem install bundler
gem install
Here is the way I fix the "invalid date format in specification" error:
1.) Go to the specifications folder located at:
/usr/local/lib/ruby/gems/1.8/specifications/
2.) Find the spec that is causing the problem.
3.) Change "s.date = %q{2011-05-21 00:00:00.000000000Z}" to "s.date = %q{2011-05-21}"
That's a WIN for me! Good Luck
You may be using old version of gem. See you current version gem -v.
Upgrade you gem to latest
gem update --system
And install you gem file again with new gem. gem install <gem-name> or bundle install

Ruby: How to include/install .bundle?

I'm new to Ruby. I have a .bundle file. I put it in the source folder and did
require('my.bundle')
But when I call the methods in the bundle, the definition is not found. Do I have to install them or include them in some other way to access them?
I am on Ruby version 1.8.7 (latest version on Mac).
I highly recommend using RVM to manage your Ruby installation, including your gems, so if you don't already have that, get it and follow the instructions for installing it. Make sure you do the part about modifying your bash startup script or you'll see weird behavior, like the wrong Ruby being called. Also, use the steps in "RVM and RubyGems" to install your gems or you can run into weird behavior with gems being installed under the wrong or an unexpected Ruby.
Second, use the gem command to install gems:
gem install gem_to_install
replacing "gem_to_install" with the name of the gem you want, and it will be installed into the appropriate gem folder for your Ruby.
If you are on Ruby 1.92, and trying to require a gem to use as a module in your code, use:
require 'gemname'
if it is installed via the gem command. And, if it is a module you wrote or have in your program's directory or below it, use:
require_relative 'path/to/gem/gemname'
If you are on a Ruby < 1.9 you'll also need to add require 'rubygems' above your other require lines, then use require './path/to/gem/gemname'.
Thanks, but my .bundle is not in gems. How do I install/require a .bundle file I already have?
If you wrote it look into rubygems/gemcutter or bundler for info on bundling and managing gems.
You can install a gem without using the app by going into the directory containing the gem and running setup.rb. See http://i.loveruby.net/en/projects/setup/doc/usage.html for a decent writeup or the official docs at: http://docs.rubygems.org/read/chapter/3

Manually adding a Ruby Gem

I am trying to install the mechanize gem that is supposed to work with 1.9 from here: http://github.com/kemiller/mechanize but I do not know how to add it manually.
I am using Windows, I could just copy the folder to the gems directory, but how do I initialize it?
I'm not sure I understand the problem. gem install mechanize doesn't work? It produces version 0.9.3 for me, which matches the gemspec of the library you linked to.
EDIT: you're on 1.9. I knew that. Disregard my hasty post, not familiar enough with Windows to offer any help on building the extensions.
I would use the bundler gem using the command gem install bundler. This will create a file called Gemfile in your project directory where you can put your dependencies for the specific project that you are working on. In the Gemfile, you will need to specify gem mechanize. If you want a specific version include ~> VERSION after. After, run the command bundle install. This will install the gem you want and use it in your project.

Resources