Class: TRuby::RubyVersion

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/t_ruby/ruby_version.rb

Overview

Value object representing a Ruby version with comparison and feature detection

Examples:

version = RubyVersion.parse("3.4")
version.supports_it_parameter? # => true
version >= RubyVersion.parse("3.0") # => true

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor, patch = 0) ⇒ RubyVersion

Returns a new instance of RubyVersion.

Parameters:

  • major (Integer)

    major version number

  • minor (Integer)

    minor version number

  • patch (Integer) (defaults to: 0)

    patch version number (default: 0)



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

#majorObject (readonly)

Returns the value of attribute major.



24
25
26
# File 'lib/t_ruby/ruby_version.rb', line 24

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



24
25
26
# File 'lib/t_ruby/ruby_version.rb', line 24

def minor
  @minor
end

#patchObject (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

.currentRubyVersion

Get the current Ruby version from the environment

Returns:



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

Parameters:

  • version_string (String, Numeric)

    version string (e.g., “3.4”, “3.4.1”)

Returns:

Raises:

  • (ArgumentError)

    if version format is invalid



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

Parameters:

Returns:

  • (Integer)

    -1, 0, or 1



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+)

Returns:

  • (Boolean)

    true if numbered parameters cause errors



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)

Returns:

  • (Boolean)

    true if version is supported



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+)

Returns:

  • (Boolean)

    true if anonymous block forwarding is supported



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+)

Returns:

  • (Boolean)

    true if ‘it` parameter is supported



94
95
96
# File 'lib/t_ruby/ruby_version.rb', line 94

def supports_it_parameter?
  self >= self.class.parse("3.4")
end

#to_sString

Convert to string representation

Returns:

  • (String)

    version string (e.g., “3.4” or “3.4.1”)



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

Returns:

Raises:



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