Monday, 24th February 2025
🔗 Dynamic routing to a controller’s action (#).
If you have a rails form which has multiple submit buttons such as Publish
and Unpublish
, you can easily route those to corresponding actions within the controller with this clever hack.
🔗 A programmable wireless eink display (#). This is a clever little product: it’s an eink display that is fully programmable and you can wirelessly update. I’m sure I could use this for a future project.
💡 TIL: Elegant solution to versioning an API #
The couple times I’ve done API versioning it was with /v1/ or v1.domain.com. Both of these are the generally recommended pattern within rails. This article outlines a querystring based approach to API versioning and a much more flexible implementation strategy that avoids a lot of duplication and code maintenance.
💡 TIL: Regex within string square brackets #
I’ve always known that Ruby supports ranges within a string’s square brackets. So in addition to the simple `name[4]` I can do `name[2..4]` and `name[2...]` and `name[-1]`. But I just learned you can put regex in the square brackets for more dynamic substring selectors: `“me+abc123@email.com”[/.+\+(.+)@(.+)/, 2]` will give you the subdomain of the email address. It even works with named capture groups! `“123.45”[/(?<dollars>\d+)\.(?<centers>\d+)/, :dollars]` will give you the dollars. The article has a few other notable examples.
💡 TIL: Ruby’s clamp method reduces conditionals #
You can constrain a variable to be within a range using clamp:
5.clamp(1, 10) # => 5 -3.clamp(1, 10) # => 1 15.clamp(1, 10) # => 10