Semantic::Version
Inherits Comparable < Reference < Object
A small Crystal utility class for storing, parsing, and comparing SemVer-style version strings.
Constants
Constructors
Initialize a new Semantic::Version from a version string.
The string must be a valid SemVer string, containing at least major, minor, and patch elements. Pre and build elements are optional.
Examples of valid strings:
"2.4.6"
"25.58.100"
"1.0.0-pre1"
"1.0.0+build.112358"
"2.0.5-alpha+prerelease"
Initialize a new Semantic::Version from a hash.
The hash must contain "major", "minor", and "patch" keys with values. "Pre" and "build" elements are optional. The hash may contain other keys, too. Those are discarded on initialization.
Example:
hash = {
"major" => 2,
"minor" => 4,
"patch" => 0,
}
version = Semantic::Version.new(hash)
Initialize a new Semantic::Version from a tuple T(Int32, Int32, Int32).
Initialize a new Semantic::Version from a tuple T(Int32, Int32, Int32, String?, String?).
Initialize a new Semantic::Version from a named tuple.
Example:
tuple = {
major: 2,
minor: 4,
patch: 0,
pre: nil,
build: "prerelease"
}
Semantic::Version.new(tuple)
Instance methods
Convenience method for testing whether a version is equal to supplied version.
Convenience method for testing whether a version is equal to supplied version string.
Convenience method for testing whether a version is greater than supplied version.
Convenience method for testing whether a version is greater than supplied version string.
Convenience method for testing whether a version is greater or equal than supplied version.
Convenience method for testing whether a version is greater or equal than supplied version string.
Convenience method for testing whether a version is less than supplied version.
Convenience method for testing whether a version is less than supplied version string.
Convenience method for testing whether a version is less or equal than supplied version.
Convenience method for testing whether a version is less or equal than supplied version string.
Bumps up major version number and returns a new Semantic::Version instance.
Note: this will clear pre and build version information for the new instance.
Bumps up minor version number and returns a new Semantic::Version instance.
Note: this will clear pre and build version information for the new instance.
Queries whether a version string with a constraint operator is satisfied with the instance.
Supported constraints are wildcard (*) and tilde (~) version ranges. Examples:
version = Semantic::Version.new("2.4.8")
version.satisfies?("2.*") # => true, >= 2.0.0 and < 3.0.0
version.satisfies?("2.4.*") # => true, >= 2.4.0 and < 2.5.0
version.satisfies?("~2.3") # => true, >= 2.3.0 and < 3.0.0
version.satisfies?("~2.3.6") # => false, >= 2.3.6 and < 2.4.0
Returns an array of the elements of the version.
Semantic::Version.new("2.4.0-pre1+build.1123").to_a # => [2, 4, 0, "pre1", "build.1123"]
Returns version information as a hash.
Semantic::Version.new("2.4.0-pre1+build.1123").to_h
# => {"major" => 2, "minor" => 4, "patch" => 0, "pre" => "pre1", "build" => "build.1123"}
Returns a version string.
Semantic::Version.new("2.4.0-pre1+build.1123").to_s # => "2.4.0-pre1+build.1123"
Returns version information as a tuple.
Semantic::Version.new("2.4.0-pre1+build.1123").to_t
# => {major: 2, minor: 4, patch: 0, pre: "pre1", build: "build.1123"}