Help us understand the problem. What is going on with this article?

バージョン番号比較

More than 3 years have passed since last update.

知らなかったので使うものだけ調べた。

PHP

まんま、バージョン番号の比較を行うための version_compare という関数があるのでそれ使うと良い。

php > var_dump(version_compare('1.7.10', '1.7.2'));
int(1)

php > var_dump(version_compare('1.7.10', '1.7.2', '>='));
bool(true)

php > var_dump(version_compare('1.7.10', '1.7.10-dev', '>='));
bool(false)

"-dev" とかついてると、開発中だからバージョン低いよねって判定されるみたい。

第3引数に渡した比較演算子使って、第1、第2引数のバージョン文字列を比較できるのは便利げ。

Ruby

Gem::Version オブジェクトにして比較できる。

irb> Gem::Version.create('1.7.10') >= Gem::Version.create('1.7.2')
=> true

irb> Gem::Version.create('1.7.10') >= Gem::Version.create('1.7.10-dev')
ArgumentError: Malformed version number string 1.7.10-dev
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/version.rb:191:in `initialize'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/version.rb:182:in `new'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/version.rb:182:in `create'
    from (irb):1
    from /usr/bin/irb:12:in `<main>'

irb> Gem::Version.create('1.7.10') >= Gem::Version.create('1.7.10.dev')
=> true

"-dev" みたいな文字列くっつけると、ちゃんと入れろやと怒られる。
".dev" みたいにすると OK。こちらも開発中だよねって感じにバージョン小さいとみなされるみたい。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away