Ruby already solved my problem šŸ˜…

3 min read Original article ↗

Yesterday I hosted November’s Hotwire Native Office Hours.

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

The big advantage this class has over my implementation is that it’s built into Ruby!

It’s not even a Rails dependency but part of the standard Ruby library. Internally, this class is used to compare version numbers when parsing your Gemfile. Check out the documentation, I picked up a few things on semantic versioning when reading it.

I’ve already replaced my AppVersion class with Gem::Version. But it makes me wonder what other Ruby/Rails features I don’t know about and am implementing on my own!

I never would have learned about Gem::Version if it wasn’t for someone bringing it up during office hours. I’d still be using my custom (and most likely buggy!) AppVersion implementation… like a chump. šŸ˜…

But seriously, this is why I love connecting with folks in the Ruby/Rails community. Every time I go to an event, host a workshop, give a talk… I learn something new. Sometimes it’s small things like Gem::Version. Other times it completely changes my career, like the first time I heard about Hotwire Native.

I’ve taken this to heart and recently started organizing monthly Coffee and Code coworking sessions in my city, Portland, OR. Every month I post up at a local coffee shop with my laptop and invite all of the Portland Ruby Brigade to join.

This month we had five people! It’s no corporate-sponsored-meetup but it sure is something. And the connections that folks are making during these casual events are real. I’ve already seen folks exchange emails for potential future contract gigs.

What’s the first step you can take today to build some community in your local area? Even if it is just finally inviting that connection-to-be for a beverage… I say go for it.

Discussion about this post

Ready for more?