Ruby 解决了我的问题。
Ruby already solved my problem

原始链接: https://newsletter.masilotti.com/p/ruby-already-solved-my-problem

作者最近为开发者举办了一期名为“Hotwire Native Office Hours”的Zoom会议,内容涵盖了基础设置到高级Apple Watch认证等主题。会议的一个重要收获是发现了Ruby内置的`Gem::Version`类。 最初,作者创建了一个自定义的`AppVersion`类来解析和比较用户代理字符串中发送的应用版本,用于功能标记。然而,一位参与者指出了`Gem::Version`,它能完成相同的任务,*并且*能处理预发布版本——所有这些都包含在标准的Ruby库中! 这次经历凸显了社区互动和持续学习的价值。作者强调了与同行开发者建立联系的好处,并因此组织了俄勒冈州波特兰市的“Coffee and Code”联合办公活动,以促进有价值的联系和潜在机会。他们鼓励大家积极在自己的领域内建立社区,即使是像邀请同事喝咖啡这样的小步骤。

## Ruby 的优雅重现 一篇最近的博客文章引发了 Hacker News 上关于 Ruby 持续吸引力的讨论。作者强调了 `AppVersion` 类简洁易读的实现,展示了即使多年未使用,Ruby 仍然优雅。 评论者们表达了相同的观点,指出 Ruby 能够用最少的代码实现清晰——这与 TypeScript、Elixir、C#、Python 和 Go 等通常需要更冗长解决方案的语言形成对比。然而,讨论也涉及了 Ruby 的性能限制以及可读性和效率之间的权衡。一些人认为,Ruby 对开发者体验的关注有时会以牺牲速度为代价,需要持续优化。 其他人指出了 Ruby 的动态特性以及健全测试的重要性,同时也承认了在大型项目中维护代码质量的挑战。尽管存在这些担忧,许多人仍然表达了对 Ruby 表达力的喜爱及其强大的元编程能力,尤其是与现代 JavaScript 框架的复杂性相比。最终,这场对话重申了 Ruby 作为一种设计良好的语言的持久价值,独立于 Rails 生态系统。
相关文章

原文

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

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! 👋

联系我们 contact @ memedata.com