0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ruby 4.0 の Ruby モジュールの定数

Posted at

Ruby 4.0 で,Ruby というモジュールが導入された。
ここにはいくつかの定数が定義されている。
これらがどんな名前と値を持っているのか,プレビュー版(4.0.0-preview3)で見てみた。

const_names = Ruby.constants
max_length = const_names.map{ _1.length }.max
format = "%#{ max_length }s %s"

const_names.each do |name|
  puts format % [name, Ruby.const_get(name).inspect]
end

結果:

    PATCHLEVEL -1
           Box Ruby::Box
      REVISION "cfa3e7cf75531a8c90b39bbd9b977f30315b12e7"
     COPYRIGHT "ruby - Copyright (C) 1993-2025 Yukihiro Matsumoto"
        ENGINE "ruby"
ENGINE_VERSION "4.0.0"
       VERSION "4.0.0"
  RELEASE_DATE "2025-12-18"
   DESCRIPTION "ruby 4.0.0preview3 (2025-12-18 master cfa3e7cf75) +PRISM [arm64-darwin25]"
      PLATFORM "arm64-darwin25"

Ruby のバージョンを得るのに,Ruby::VERSION という定数が使えるのか。
しかしこれの値はただの文字列で,従来からある RUBY_VERSION と同じ値だ(オブジェクトとしても同じ):

p Ruby::VERSION.equal?(RUBY_VERSION)
# => true

gem のバージョンを表すのには Gem::Version というクラスがあるが,Ruby のバージョンにはそういう専用のクラスが無いのだ(4.0 で導入されるのかと思ったけど,そんなことはなかった)。

昔からの素朴な疑問だけど,文字列でバージョンを表すと,2 桁になったとき大小比較で困るのでは?

p "4.0.2" < "4.0.10"
# => false
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?