Making use of Ruby 3 destructuring
Ruby 3 has some nice set of features, but destructuring is something that I am using a lot. Noting few of them here.
With Ruby 3 we have hash destructuring based on improved pattern matching.
Tried on ruby version ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [arm64-darwin21]
What we used to do
We could destructure an array like this
But for hashes the below did not work
We could although do this
but then we get an array as a result and not the best experience.
What we can do now
In ruby 3 with rightward assignment operator we can do
Also, if not found
But this a runtime error, so we need to rescue this
We can also do rightward assignment as well
And one the best use cases
Looking at you JS 😏
Another sample usage
Further Reading
There are more use cases and patterns, which can be seen in the docs.
Updated on