# struct SemanticVersion

- SemanticVersion
- Struct
- Value
- Object

##
Overview

Conforms to Semantic Versioning 2.0.0

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

##
Included Modules

- Comparable(SemanticVersion)

##
Defined in:

semantic_version.cr

##
Constant Summary

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

- .parse(str : String) : self

  Parses a `SemanticVersion` from the given semantic version string.

##
Class Method Summary

- .parse?(str : String) : self | Nil

  Parses a `SemanticVersion` from the given semantic version string.

- .valid?(str : String) : Bool

  Checks if str is a valid semantic version.

##
Instance Method Summary

- #<=>(other : self) : Int32

  The comparison operator.

- #==(other : self)
- #build : String | Nil

  The build metadata of this semantic version

- #bump_major

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

- #bump_minor

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

- #bump_patch

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

- #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.

- #hash(hasher)

  See `Object#hash(hasher)`

- #major : Int32

  The major version of this semantic version

- #minor : Int32

  The minor version of this semantic version

- #patch : Int32

  The patch version of this semantic version

- #prerelease : Prerelease

  The pre-release version of this semantic version

- #to_s(io : IO) : Nil

  Returns the string representation of this semantic version

### Instance methods inherited from module `Comparable(SemanticVersion)`

<(other : T) : Bool <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) : Bool >, >=(other : T) >=, clamp(min, max)
clamp(range : Range) clamp

### Instance methods inherited from struct `Struct`

==(other : YAML::Any)
==(other) : Bool ==, hash(hasher) hash, inspect(io : IO) : Nil inspect, pretty_print(pp) : Nil pretty_print, to_s(io : IO) : Nil to_s

### Class methods inherited from struct `Struct`

pre_initialize(address : Pointer) : Nil pre_initialize

### Instance methods inherited from struct `Value`

==(other : Log::Metadata::Value)
==(other : JSON::Any)
==(other : YAML::Any)
==(other) ==, dup dup

### Instance methods inherited from class `Object`

! : Bool !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other) ===, =~(other) =~, as(type : Class) as, as?(type : Class) as?, class class, dup dup, hash(hasher)
hash hash, in?(collection : Object) : Bool
in?(*values : Object) : Bool in?, inspect(io : IO) : Nil
inspect : String inspect, is_a?(type : Class) : Bool is_a?, itself itself, nil? : Bool nil?, not_nil!(message)
not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, responds_to?(name : Symbol) : Bool responds_to?, tap(&) tap, to_json(io : IO) : Nil
to_json : String to_json, to_pretty_json(indent : String = " ") : String
to_pretty_json(io : IO, indent : String = " ") : Nil to_pretty_json, to_s(io : IO) : Nil
to_s : String to_s, to_yaml(io : IO) : Nil
to_yaml : String to_yaml, try(&) try, unsafe_as(type : T.class) forall T unsafe_as

### Class methods inherited from class `Object`

from_json(string_or_io : String | IO, root : String)
from_json(string_or_io : String | IO) from_json, from_yaml(string_or_io : String | IO) from_yaml

### Macros inherited from class `Object`

class_getter(*names, &block) class_getter, class_getter!(*names) class_getter!, class_getter?(*names, &block) class_getter?, class_property(*names, &block) class_property, class_property!(*names) class_property!, class_property?(*names, &block) class_property?, class_setter(*names) class_setter, def_clone def_clone, def_equals(*fields) def_equals, def_equals_and_hash(*fields) def_equals_and_hash, def_hash(*fields) def_hash, delegate(*methods, to object) delegate, forward_missing_to(delegate) forward_missing_to, getter(*names, &block) getter, getter!(*names) getter!, getter?(*names, &block) getter?, property(*names, &block) property, property!(*names) property!, property?(*names, &block) property?, setter(*names) setter

##
Constructor Detail

def self.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

def self.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.

##
Class Method Detail

def self.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.

def self.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

##
Instance Method Detail

def <=>(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

def ==(other : self) #
def build : String | Nil #

The build metadata of this semantic version

def 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=[]))

def 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=[]))

def 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=[]))

def 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=[]))

def hash(hasher) #
Description copied from struct Struct

See `Object#hash(hasher)`

def major : Int32 #

The major version of this semantic version

def minor : Int32 #

The minor version of this semantic version

def patch : Int32 #

The patch version of this semantic version

def prerelease : Prerelease #

The pre-release version of this semantic version

def 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"