Yesterday I hosted November’s Hotwire Native Office Hours.
Every month I host an hour long Zoom session for developers to directly ask me questions. The topics range greatly: some folks are just getting started and others are asking very specific, advanced questions.
This month we covered everything from registering bridge components to native vs. web-based tabs to authenticating Apple Watch apps! It’s really fun to see what folks are working on in the Hotwire Native space.
During the session I shared some code I wrote to figure out what version a Hotwire Native app is running. The app sends the version in the user agent (e.g. v1.2.3 ) and then I parse it with the following Ruby class on the server:
class AppVersion include Comparable attr_reader :major, :minor, :patch def initialize(version_string) parts = version_string.to_s.split(”.”).map(&:to_i) @major, @minor, @patch = parts[0] || 0, parts[1] || 0, parts[2] || 0 end def <=>(other) [major, minor, patch] <=> [other.major, other.minor, other.patch] end def to_s “#{major}.#{minor}.#{patch}” end end
And it works great! I use this throughout my apps to feature flag code based on which version the app is running.
But someone brought up something even better: Gem::Version . This class accomplishes the same goal: “process string versions into comparable values”.
irb(main)> Gem::Version.new("1.2.3") > Gem::Version.new("1.2.2") => true irb(main)> Gem::Version.new("2.0") > Gem::Version.new("1.2.2") => true
It can even compare prerelease versions, like alphas or betas.
irb(main)> Gem::Version.new("2.0.b1") > Gem::Version.new("1.9") => true irb(main)> Gem::Version.new("2.0") > Gem::Version.new("2.0.b1") => true
... continue reading