Ruby already solved my problem ๐Ÿ˜…

โœจ Check out this must-read post from Hacker News ๐Ÿ“–

๐Ÿ“‚ Category:

๐Ÿ’ก Key idea:

Yesterday I hosted Novemberโ€™s Hotwire Native Office Hours.

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
    โ€œ#โšก.#Tell us your thoughts in comments!.#๐Ÿ’ฌโ€
  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.

If you want to join next monthโ€™s office hours then consider becoming a paid subscriber of my newsletter. I hope to see you there! ๐Ÿ‘‹

Hotwire Native Office Hours

Hotwire Native Office Hours

Every month I host an hour-long Zoom session for Hotwire Native developers to ask questions, share progress, and connect with others building the same way.

Share your opinion below! Tell us your thoughts in comments!

#๏ธโƒฃ #Ruby #solved #problem

๐Ÿ•’ Posted on 1762542899

By

Leave a Reply

Your email address will not be published. Required fields are marked *