从内部获取已执行gem的规范

时间:2021-08-27 00:19:45

I have build a gem and want it to print its version.

我已经构建了一个gem并希望它打印它的版本。

I cant use Gem::Specification.find_by_name('mygem').version because there are several versions of it installed.

我不能使用Gem :: Specification.find_by_name('mygem')。version因为它安装了几个版本。

Lets just say my program has just a single src file /bin/myruby containing this:

让我们说我的程序只有一个src文件/ bin / myruby包含这个:

#!/usr/bin/env ruby 
mygem_version = ???
puts "This is my gems version: #{mygem_version}"

3 个解决方案

#1


3  

A common convention is to create a lib/<your_gem_name>/version.rb that defines a YourGemName::VERSION constant. Then, you can refer to that constant in your gemspec, which is Ruby code that gets evaluated when the gem is built.

常见的约定是创建一个定义YourGemName :: VERSION常量的lib / /version.rb。然后,您可以在gemspec中引用该常量,这是在构建gem时得到的Ruby代码。

Read http://timelessrepo.com/making-ruby-gems for a guide that uses this approach.

请阅读http://timelessrepo.com/making-ruby-gems以获取使用此方法的指南。

#2


0  

If you're using Bundler (think rails), try

如果您正在使用Bundler(想想rails),请尝试

Bundler.definition.specs

else, make sure your gem has a VERSION constant you can ask these things

否则,确保你的宝石有一个VERSION常数你可以问这些东西

#3


0  

This worked for me and always returned the correct version.

这对我有用,并且总是返回正确的版本。

#!/usr/bin/env ruby 
puts "This is my gems version: #{Gem.loaded_specs['mygem'].version}"

#1


3  

A common convention is to create a lib/<your_gem_name>/version.rb that defines a YourGemName::VERSION constant. Then, you can refer to that constant in your gemspec, which is Ruby code that gets evaluated when the gem is built.

常见的约定是创建一个定义YourGemName :: VERSION常量的lib / /version.rb。然后,您可以在gemspec中引用该常量,这是在构建gem时得到的Ruby代码。

Read http://timelessrepo.com/making-ruby-gems for a guide that uses this approach.

请阅读http://timelessrepo.com/making-ruby-gems以获取使用此方法的指南。

#2


0  

If you're using Bundler (think rails), try

如果您正在使用Bundler(想想rails),请尝试

Bundler.definition.specs

else, make sure your gem has a VERSION constant you can ask these things

否则,确保你的宝石有一个VERSION常数你可以问这些东西

#3


0  

This worked for me and always returned the correct version.

这对我有用,并且总是返回正确的版本。

#!/usr/bin/env ruby 
puts "This is my gems version: #{Gem.loaded_specs['mygem'].version}"