Published November 2, 2019 Updated May 11, 2020
Demonstrates the Ruby Versionaire gem for building and using semantic versions.
# Hello and welcome to the Alchemists Screencasts! # Today, we'll look at the Versionaire gem: # https://alchemists.io/projects/versionaire # We'll explore via Versionaire's gem console: bin/console
# We'll start by creating a few objects: v1 = Versionaire::Version.new v2 = Versionaire::Version.new v3 = Versionaire::Version[major: 3, minor: 2, patch: 1] v4 = Versionaire::Version[major: 4] # We can test for value equality: v1 == v1 v1 == v2 v1 == v3 v1 == v4 # The v1 and v2 instances, despite being different objects in memory, are equal since they represent a state of "0.0.0". # v1 is not equal to v3 and v4 since they represent different values (i.e. state) for major, minor, and patch. # The same is true for hash equality: v1.eql? v1 v1.eql? v2 v1.eql? v3 v1.eql? v4 # We can verify hash equality by looking at the hash values generated for each object (used by `#eql?`): v1.hash v2.hash v3.hash v4.hash # As shown earlier via the `#eql?`, we see v1 and v2 are equal but v3 and v4 are not. # Case equality behaves the same as value and hash equality (again equality is based on the object state): v1 === v1 v1 === v2 v1 === v3 v1 === v4 # Identity equality is equal only to the same instance in memory: v1.equal? v1 v1.equal? v2 v1.equal? v3 v1.equal? v4 # We can verify identity equality by looking at the object ID of each instance (used by #equal?): v1.object_id v2.object_id v3.object_id v4.object_id # As you can see, each object has a unique ID. # For convenience, a Version function is provided for easily converting (casting) objects into versions: version = Versionaire::Version[major: 1, minor: 2, patch: 3] v1 = Versionaire::Version "1.2.3" v2 = Versionaire::Version [1, 2, 3] v3 = Versionaire::Version major: 1, minor: 2, patch: 3 v4 = Versionaire::Version version v1 == v2 && v1 == v3 && v1 == v4 # This behavior is similar to Ruby's own conversion functions such as `Integer()`, `String()`, `Array()`, etc. # Alternatively, a version can be converted to String, Array, and Hash objects: version = Versionaire::Version[major: 1, minor: 2, patch: 3] version.to_s version.to_a version.to_h # Versions can be added to one another: v1 = Versionaire::Version[major: 1, minor: 2, patch: 3] v2 = Versionaire::Version[major: 3, minor: 2, patch: 1] (v1 + v2).to_s # Subtraction works too: v1 = Versionaire::Version[major: 5] v2 = Versionaire::Version[major: 1] (v1 - v2).to_s # Note, when subtracting, an exception will be thrown if the resulting version would be a negative: v2 - v1 exit
# That is Versionaire in a nutshell. # Enjoy! # https://alchemists.io # ☿ 🜔 🜍 🜂 🜃 🜁 🜄