The basic principle of ruby 3 is to keep compatibility and make the language faster and more productive. Thus, the main three aspects of the new version are making ruby fast, concurrent, and correct.
The speed is achieved by improving the performance of MJIT. As a result, ruby runs three times faster than previous versions in some benchmarks, though not with the rails applications.
In this multi-core age, concurrency is gained with Async I/O Fiber and Ractor.
- Async I/O Fiber: A fiber for heavy I/O tasks. This doesn’t utilize the multi-core but utilizes blocking time to improve the performance of I/O tasks. This fiber is based on the technology used by the Ruby application server - Falcon.
Ractor: It stands for Ruby Actor which helps in improving CPU intensive tasks. There are isolated object spaces for each ractor and they communicate via channels that enable parallel execution without safety concerns. You can make multiple ractors and you can run them in parallel. Ractor enables you to make thread-safe parallel programs because ractors can not share normal objects. Communication between ractors is supported by exchanging messages.
Ruby seeks the future with static type checking, without type declaration, using abstract interpretation. RBS & Type Profiler are the first step in the future.
- RBS (Ruby Signature): It is a language similar to Ruby, but specially designed to describe types. Ruby 3 ships with RBS for the core library (RBS string, RBS hash, and so on). We can use this type of information in RBS files for type checking and better IDE with better code compilation.
Type Profiler: It does naive type checks, also generates RBS for your application using abstract interpretation. If you have some type of mismatch in your application, the type profiler will warn you about these errors.
How does it work?
The first type profiler will check the new instance of Test which calls test().
Now, test() is bound to value 15 which is an integer.
So, it can be said that a is an integer, meaning the test method takes integer input.
Now, a has an addition (+) operation with 2 (again integer) which will return an integer. So, b is an integer.
Lastly, it can be concluded that test() returns an integer.
RBS file for Test:
- ' => ' symbol is added which can be used as rightward assignment operator(right assignment)
- Now ‘in’ returns true or false
The inline method definition is added.
except() function is added for hash