简体   繁体   中英

How to modify a Ruby gem

How can I modify an existing *.gem file? I want to modify a Rakefile in a gem so that it will be able to compile on Windows.

Download its source code into a separate folder (perhaps from github). Then modify your Gemfile to point to the source directly so that you can edit it and test your changes directly.

For example, let's say that you want to edit the secure_headers gem and that you've cloned it into ~/workspace/secureheaders. Then you can use the following in your Gemfile:

gem 'secure_headers', :path => "~/workspace/secureheaders"

Then, just modify the source files inside the source tree and you should be able to see your changes applied directly.

You can gem unpack it, add your modification, then modify the gemspec if necessary and build it again:

Newer versions of bundler allow you to do this with the following command:

bundle open gem_to_edit

This will open the gem code in your default Text editor.

(Hint: if you are on Mac OSX/Linux, you can specify your default text editor by changing the $EDITOR variable. Say you are on bash, you could just open your ~/.bash_profile and add: export EDITOR='subl -w' to set up Sublime Text as your default editor)

Perhaps a more acceptable and open source friendly way to do this would be to check to see if the gem's source is available on something like github. Then create a fork of the gem code, make your changes, and send a pull request. Chances are, someone else wants that functionality too and you will be heralded for your contribution.

If you lack a gemspec for the gem, extract it with this command:

gem spec my_gem --ruby > my_gem.gemspec

Of course replacing my_gem with the appropriate gem name. Edit the resulting file to fit (in one example I had to delete a line that caused an error), and you should be good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM