为什么使用嵌套的Ruby模块获取版本信息? [英] Why use a nested Ruby module for version information?

查看:94
本文介绍了为什么使用嵌套的Ruby模块获取版本信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在看几个宝石的源代码。我一直看到的一个习惯用法是使用一个包含版本常量的嵌套模块,这些常量被加入到一个版本字符串中,也就是与这种事情有关的变体:

 模块ChunkyBacon 
模块版本
MAJOR = 0
MINOR = 6
TINY = 2
结束

VERSION = [ Version :: MAJOR,Version :: MINOR,Version :: TINY] .compact *'。'
end

以这种方式存储库版本信息的优点(如果有的话)是什么?为什么不这样做:

  module ChunkyBacon 
VERSION ='0.6.2'.freeze
end


解决方案

对于程序兼容性检查,版本号。



例如:

 要求'chunkybacon'
if ChunkyBacon :: VERSION :: MAJOR> 0
#barf
结束


I've been looking at the source code of a few gems lately. One idiom that I keep seeing is the use of a nested module containing version constants that are joined into a version string i.e. variations around this sort of thing:

module ChunkyBacon
  module Version
    MAJOR = 0
    MINOR = 6
    TINY  = 2
  end

  VERSION = [Version::MAJOR, Version::MINOR, Version::TINY].compact * '.'
end

What's the advantage (if any) of storing the library version information in this way? Why not just do:

module ChunkyBacon
  VERSION = '0.6.2'.freeze
end

解决方案

For programmatic compatibility checks, it's easier to have an explicitly separated version number.

e.g.:

require 'chunkybacon'
if ChunkyBacon::VERSION::MAJOR > 0
  # barf
end

这篇关于为什么使用嵌套的Ruby模块获取版本信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆