struct

SemanticVersion

Inherits Comparable / Struct / Value / Object

Conforms to Semantic Versioning 2.0.0

See https://semver.org/ for more information.

Constants

VERSION_PATTERN = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*) (?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))? (?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/x

Constructors

new(major : Int, minor : Int, patch : Int, prerelease : String | Prerelease | Nil = nil, build : String | Nil = nil)

Creates a new SemanticVersion instance with the given major, minor, and patch versions and optionally build and pre-release version

Raises ArgumentError if prerelease is invalid pre-release version

Source
parse(str : String) : self

Parses a SemanticVersion from the given semantic version string.

require "semantic_version"

semver = SemanticVersion.parse("2.61.4")
semver # => #<SemanticVersion:0x55b3667c9e70 @major=2, @minor=61, @patch=4, ... >

Raises ArgumentError if str is not a semantic version.

Source

Class methods

parse?(str : String) : self | Nil

Parses a SemanticVersion from the given semantic version string.

require "semantic_version"

semver = SemanticVersion.parse?("2.61.4")
semver # => #<SemanticVersion:0x55b3667c9e70 @major=2, @minor=61, @patch=4, ... >

Returns nil if str is not a semantic version.

Source
valid?(str : String) : Bool

Checks if str is a valid semantic version.

require "semantic_version"

SemanticVersion.valid?("1.15.0") # => true
SemanticVersion.valid?("1.2")    # => false
Source

Instance methods

<=>(other : self) : Int32

The comparison operator.

Returns -1, 0 or 1 depending on whether self's version is lower than other's, equal to other's version or greater than other's version.

require "semantic_version"

semver1 = SemanticVersion.new(1, 0, 0)
semver2 = SemanticVersion.new(2, 0, 0)

semver1 <=> semver2 # => -1
semver2 <=> semver2 # => 0
semver2 <=> semver1 # => 1
Source
==(other : self)
build

The build metadata of this semantic version

Source
bump_major

Returns a copy of the current version with a major bump.

require "semantic_version"

current_version = SemanticVersion.new 1, 1, 1, "rc"
current_version.bump_major # => SemanticVersion(@build=nil, @major=2, @minor=0, @patch=0, @prerelease=SemanticVersion::Prerelease(@identifiers=[]))
Source
bump_minor

Returns a copy of the current version with a minor bump.

require "semantic_version"

current_version = SemanticVersion.new 1, 1, 1, "rc"
current_version.bump_minor # => SemanticVersion(@build=nil, @major=1, @minor=2, @patch=0, @prerelease=SemanticVersion::Prerelease(@identifiers=[]))
Source
bump_patch

Returns a copy of the current version with a patch bump. Bumping a patch of a prerelease just erase the prerelease data.

require "semantic_version"

current_version = SemanticVersion.new 1, 1, 1, "rc"
next_patch = current_version.bump_patch # => SemanticVersion(@build=nil, @major=1, @minor=1, @patch=1, @prerelease=SemanticVersion::Prerelease(@identifiers=[]))
next_patch.bump_patch                   # => SemanticVersion(@build=nil, @major=1, @minor=1, @patch=2, @prerelease=SemanticVersion::Prerelease(@identifiers=[]))
Source
copy_with(major : Int32 = @major, minor : Int32 = @minor, patch : Int32 = @patch, prerelease : String | Prerelease | Nil = @prerelease, build : String | Nil = @build)

Returns a new SemanticVersion created with the specified parts. The default for each part is its current value.

require "semantic_version"

current_version = SemanticVersion.new 1, 1, 1, "rc"
current_version.copy_with(patch: 2)        # => SemanticVersion(@build=nil, @major=1, @minor=1, @patch=2, @prerelease=SemanticVersion::Prerelease(@identifiers=["rc"]))
current_version.copy_with(prerelease: nil) # => SemanticVersion(@build=nil, @major=1, @minor=1, @patch=1, @prerelease=SemanticVersion::Prerelease(@identifiers=[]))
Source
hash(hasher)

See Object#hash(hasher)

major

The major version of this semantic version

Source
minor

The minor version of this semantic version

Source
patch

The patch version of this semantic version

Source
prerelease

The pre-release version of this semantic version

Source
to_s(io : IO) : Nil

Returns the string representation of this semantic version

require "semantic_version"

semver = SemanticVersion.parse("0.27.1")
semver.to_s # => "0.27.1"
Source

Nested types