Ruby 3.0 adds
After the last four or five years of promises of a new major version of Ruby (Ruby 3), it looks like this new version is set to be released on December 25, 2020. This article will look at one new feature.
Goals
There are three main goals for this future release.
- Speed: implementing its long-awaited async i/o “fibers” as a better way to control asynchronous threads.
- Concurrency: implementing “Ractors” (“Ruby Actors”) — similar to the way JavaScript offers background “web worker” scripts.
- Being Correct: Ruby 3 will ship with type signatures for its core libraries, available for both type checking and enhancing future IDEs.
New Hash method
Ruby 3 will add a new method for a hash #except, which returns a new hash.
Assuming the hash: {food: "Burger", beverage: "Coffee", fries: "large"}, in Rails 6, we can exclude an item like so:
{food: "Burger", beverage: "Coffee", fries: "large"}.except(:fries)
and it returns the hash: {food: "Burger", beverage: "Coffee"}
Currently, in the latest version of Ruby, you can use slice to build the new hash you want, a different approach to yield the same results:
{food: "Burger", beverage: "Coffee", fries: "large"}.slice(:food, :beverage)
and it returns the hash: {food: "Burger", beverage: "Coffee"}
*Ruby 3
Now with Ruby 3, you can use the new #except method: {food: "Burger", beverage: "Coffee", fries: "large"}.except(:fries)
Also, you can exclude multiple items: {food: "Burger", beverage: "Coffee", fries: "large"}.except(:fries, :food)
Nice edition to the Ruby language with more to come. Stay tuned for more.