💡 The Tip of the Week is back! If you missed last week's, feel free to step back in time, but otherwise this week's is at the end of the issue, as before. You might also want to follow Jemma on Twitter as she's currently writing a book about garbage collection and has posted some interesting tweets so far. __ Peter Cooper, editor | |
RBS: The New Ruby 3 Typing Language in Action— We briefly covered what RBS was when Ruby 3.0 launched but.. it’s not the easiest thing to get your head around. This article goes into more depth and is a worthwhile introduction (and comparison to Sorbet) if typed Ruby intrigues you. Diogo Souza |
Complete Peace of Mind Rails Hosting— If you need 100% peace of mind Rails hosting, OpsCare is for you. Built on our custom stack with comprehensive yet flexible deployment, scaling and monitoring tools, we keep your application online and performing, 24 hours a day, 7 days a week. OpsCare by reinteractive sponsor |
Logidze 1.0: Active Record, Postgres, Rails, and Time Travel— The rather tricky to pronounce Logidze is a library that tracks changes made to tables managed by Active Record by way of Postgres triggers and a JSONB record. It proudly claims to be the ‘fastest gem for auditing models in the Rails ecosystem.’ Vladimir Dementyev |
Object#tap And How To Use It— I think my favorite part of this explanation of this oft-confusing method is the reasoning behind its name, which now makes perfect sense.
Tom Dalling |
What Week Is It Anyway?— Working with dates and times is hard. Time zones and mind-bending edge cases make tests flaky and bugs hard to reproduce. This two-part series walks through two such cases. Did you know that some years have 53 weeks? Barbara Litwińska |
Find Your Next Job Through Hired— Create a profile on Hired to connect with hiring managers at growing startups and Fortune 500 companies. It's free for job-seekers. Hired |
ℹ️ Interested in running a job listing in Ruby Weekly? There's more info here. |
💡 Tip of the Week Using Comparable#clamp to limit a value to within a range Forcing a value to fall within a range became a whole lot easier when Ruby 2.4 introduced Comparable#clamp . Comparable#clamp takes min and max arguments. If the value we're "clamping" is within the min and max , Comparable#clamp returns the value unchanged. Otherwise, if the value is less than min , Comparable#clamp returns min , and if the value is greater than max , Comparable#clamp returns max :
|
1.clamp(0,3)
=> 1
1.clamp(2,3)
=> 2
"jemma".clamp("a", "z")
=> "jemma"
"jemma".clamp("a", "j")
=> "j"
|
And then, just when we were getting really excited about Comparable#clamp , Ruby 2.7 gave us even more to work with! As of Ruby 2.7, clamp can also take an inclusive .. (not exclusive ... ) range of values. Notably, nil can be within this range. val.clamp(..max) will be the equivalent of taking the minimum of val and max . And vice versa. Let's take a look: |
1.clamp(2..3)
=> 2
1.clamp(..3)
=> 1
|
And lastly, just as a reminder, the Comparable mixin is used by classes whose objects can be comparaed and therefore ordered. If you'd like to have clamp ing work for a class you've written, be sure to include Comparable and define the <=> operator. This week’s tip was written by Jemma Issroff. |
|
|