如何在ruby宝石中使用钩子

前端之家收集整理的这篇文章主要介绍了如何在ruby宝石中使用钩子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_0@
我有一个gem需要在安装gem之后执行一些rake任务.使用 http://blog.costan.us/2008/11/post-install-post-update-scripts-for.html中描述的方法安装后执行代码有一种hackish方式.

但是Gem类现在有钩子,其中一个是post_install钩子.我想添加一行像

Gem.post_install { puts 'post hook example' }

进入Rakefile,它似乎没有在安装过程中执行.这条线应该放在哪里以便挂钩?

解决方法

在lib / ruby​​gems_plugin.rb上创建一个文件

在此文件中,您可以定义自定义挂钩.例如:

Gem.post_install do
  puts "post_install called for gem"
end

无需任何要求.
输出示例:

Successfully built RubyGem
  Name: post_install_test
  Version: 0.1.0
  File: post_install_test-0.1.0.gem
post_install called for gem
Successfully installed post_install_test-0.1.0
1 gem installed

我在source中只找到了相关文档.

如果这不起作用,或者您对post安装挂钩的更改似乎并不总是更新,请在重建和安装之前完全卸载gem.

原文链接:https://www.f2er.com/ruby/270127.html

猜你在找的Ruby相关文章