Ruby 3.0 What to Expect?
Quick highlights
- feature: static type checking with RBS Ruby’s type signature language benefit: fewer bugs at runtime
- feature: parallelism execution with Ractor (experimental) benefit: light-weight concurrency and parallel threaded computations
- feature: scheduler interface benefit: enhance Fiber implementation for an enhanced concurrency environment
- feature: new syntax benefit: as Ruby’s creator would say “to make programming more enjoyable”
What is Ruby Signature RBS?
Simply put, it is a static typing language that will allow Ruby programmers to avoid unforeseen errors at runtime. For example:
class Authorattr_reader name: String
attr_reader address: Stringdef initialize: (name: String, address: String) -> voiddef each_author: () { (Author) -> void } -> void | () -> Enumerator[Author, void]
endend
Say we initialize the Author
class above, and then we execute a method that searches for the author’s favorite book so say Author.favorite_book
, Ruby would not return an error before runtime because it dynamically sets this new favorite_book
attribute even though it was never declared. With RBS we can now predefine our classes in such a way that allows us to much easier locate bugs in our code before runtime. Keeping true with Ruby’s creator’s motto “programming should be enjoyable.”
Concurrency & Parallelism with Ractor
Currently, Ruby is hampered by memory management issues, to understand the issues would involve a deep dive into I/O blocking and the Global Interpreter Lock (GIL), a solid reference can be found here. In essence, Ractor will try to forgive Yukihiro Matsumoto’s error in predicting that single-core machines would be the end all and be all of computer prowess. Ractor will make use of multi-core machines by enabling parallelism in multi-core machines which would drastically improve performance. A deep dive into what parallelism is can be found here.
More Concurrency?
Absolutely! According to the release notes, Ruby’s 3.0 scheduler feature “allows for light-weight concurrency without changing existing code”, and it seems to be designed as a wrapper for gems like EventMachine and Async, also Fibers, that provide asynchronous or concurrency libraries/language core features for the application. In regards to Fibers, scheduler would internally decide when to start the fiber.
Other Features
Ruby 3.0 will include a new right-handed variable assignment feature with the =>
operator, for example: 10 => x
. Also, a find pattern will be added, see the example below.
case ["a", 1, "b", "c", 2, "d", "e", "f", 3]
in [*pre, String => x, String => y, *post]
p pre #=> ["a", 1]
p x #=> "b"
p y #=> "c"
p post #=> [2, "d", "e", "f", 3]
end
Release Notes
There will be a number of compatibility breaks such as keyword argument declarations and using the splat operator to pass in arguments. For a complete list of breaking changes please view the documentation regarding breaking changes.