Class: TRuby::RubyVersion
- Inherits:
-
Object
- Object
- TRuby::RubyVersion
- Includes:
- Comparable
- Defined in:
- lib/t_ruby/ruby_version.rb
Overview
Value object representing a Ruby version with comparison and feature detection
Constant Summary collapse
- MIN_VERSION =
Supported version range
[3, 0].freeze
- MAX_MAJOR =
4- VERSION_REGEX =
Version string pattern: major.minor or major.minor.patch
/\A(\d+)\.(\d+)(?:\.(\d+))?\z/
Instance Attribute Summary collapse
-
#major ⇒ Object
readonly
Returns the value of attribute major.
-
#minor ⇒ Object
readonly
Returns the value of attribute minor.
-
#patch ⇒ Object
readonly
Returns the value of attribute patch.
Class Method Summary collapse
-
.current ⇒ RubyVersion
Get the current Ruby version from the environment.
-
.parse(version_string) ⇒ RubyVersion
Parse a version string into a RubyVersion object.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compare two versions.
-
#initialize(major, minor, patch = 0) ⇒ RubyVersion
constructor
A new instance of RubyVersion.
-
#numbered_parameters_raise_error? ⇒ Boolean
Check if numbered parameters (_1, _2, etc.) raise NameError (Ruby 4.0+).
-
#supported? ⇒ Boolean
Check if this version is within the supported range (3.0 ~ 4.x).
-
#supports_anonymous_block_forwarding? ⇒ Boolean
Check if this version supports anonymous block forwarding ‘def foo(&) …
-
#supports_it_parameter? ⇒ Boolean
Check if this version supports the ‘it` implicit block parameter (Ruby 3.4+).
-
#to_s ⇒ String
Convert to string representation.
-
#validate! ⇒ RubyVersion
Validate that this version is supported, raising an error if not.
Constructor Details
#initialize(major, minor, patch = 0) ⇒ RubyVersion
Returns a new instance of RubyVersion.
29 30 31 32 33 |
# File 'lib/t_ruby/ruby_version.rb', line 29 def initialize(major, minor, patch = 0) @major = major @minor = minor @patch = patch end |
Instance Attribute Details
#major ⇒ Object (readonly)
Returns the value of attribute major.
24 25 26 |
# File 'lib/t_ruby/ruby_version.rb', line 24 def major @major end |
#minor ⇒ Object (readonly)
Returns the value of attribute minor.
24 25 26 |
# File 'lib/t_ruby/ruby_version.rb', line 24 def minor @minor end |
#patch ⇒ Object (readonly)
Returns the value of attribute patch.
24 25 26 |
# File 'lib/t_ruby/ruby_version.rb', line 24 def patch @patch end |
Class Method Details
.current ⇒ RubyVersion
Get the current Ruby version from the environment
52 53 54 |
# File 'lib/t_ruby/ruby_version.rb', line 52 def self.current parse(RUBY_VERSION) end |
.parse(version_string) ⇒ RubyVersion
Parse a version string into a RubyVersion object
40 41 42 43 44 45 46 47 |
# File 'lib/t_ruby/ruby_version.rb', line 40 def self.parse(version_string) str = version_string.to_s match = VERSION_REGEX.match(str) raise ArgumentError, "Invalid version: #{version_string}" unless match new(match[1].to_i, match[2].to_i, (match[3] || 0).to_i) end |
Instance Method Details
#<=>(other) ⇒ Integer
Compare two versions
60 61 62 |
# File 'lib/t_ruby/ruby_version.rb', line 60 def <=>(other) [major, minor, patch] <=> [other.major, other.minor, other.patch] end |
#numbered_parameters_raise_error? ⇒ Boolean
Check if numbered parameters (_1, _2, etc.) raise NameError (Ruby 4.0+)
108 109 110 |
# File 'lib/t_ruby/ruby_version.rb', line 108 def numbered_parameters_raise_error? self >= self.class.parse("4.0") end |
#supported? ⇒ Boolean
Check if this version is within the supported range (3.0 ~ 4.x)
74 75 76 |
# File 'lib/t_ruby/ruby_version.rb', line 74 def supported? self >= self.class.parse("#{MIN_VERSION[0]}.#{MIN_VERSION[1]}") && major <= MAX_MAJOR end |
#supports_anonymous_block_forwarding? ⇒ Boolean
Check if this version supports anonymous block forwarding ‘def foo(&) … end` (Ruby 3.1+)
101 102 103 |
# File 'lib/t_ruby/ruby_version.rb', line 101 def supports_anonymous_block_forwarding? self >= self.class.parse("3.1") end |
#supports_it_parameter? ⇒ Boolean
Check if this version supports the ‘it` implicit block parameter (Ruby 3.4+)
94 95 96 |
# File 'lib/t_ruby/ruby_version.rb', line 94 def supports_it_parameter? self >= self.class.parse("3.4") end |
#to_s ⇒ String
Convert to string representation
67 68 69 |
# File 'lib/t_ruby/ruby_version.rb', line 67 def to_s patch.zero? ? "#{major}.#{minor}" : "#{major}.#{minor}.#{patch}" end |
#validate! ⇒ RubyVersion
Validate that this version is supported, raising an error if not
82 83 84 85 86 87 88 89 |
# File 'lib/t_ruby/ruby_version.rb', line 82 def validate! unless supported? raise UnsupportedRubyVersionError, "Ruby #{self}는 지원되지 않습니다. 지원 범위: #{MIN_VERSION.join(".")} ~ #{MAX_MAJOR}.x" end self end |