Hello guys, we must have heard about Ruby’s upcoming release of Ruby version 3, also known as 3×3 ruby, so what’s new in Ruby version 3, and what updates are worth it? Not to mention, and why do people call it 3×3 ruby version, then in today’s post we will go together to find out and answer the above information, and review the overview of whether this version Is there a breakthrough version like what was announced before?
1: Ruby 3 major updates
With Ruby version this time, the number 3 is really important, it is not just an ordinary 3, it is also shown through various milestones such as:
- Version Number (version 3.0)
- Faster performance (3X)
- There are 3 main core. (being faster, better, ensuring correctness.)
2: Ruby 3 performance
As you have read above, one of the main focus of Ruby 3 is performance, the development team has set a very ambitious goal of making Ruby 3x faster, on tests and statistics. Before the release, the developer has pointed this out!
With my ideology:
” I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy “.
Some people ask:
” Is the goal to make Ruby the fastest language ?”
The answer is no. The main goal of Ruby 3×3 is to make Ruby 3x faster than Ruby 2. As the Ruby language improves performance, it definitely makes our application faster and scalable. There are two areas where performance can be measured: memory and CPU.
CPU optimization
Several improvements have been made in Ruby to improve speed. Ruby has optimized the JIT (Just In Time) compiler from previous versions. The Ruby MJIT compiler was first introduced in Ruby 2.6. Ruby 3 MJIT comes with better security and seems to improve web application performance to a higher degree.
The MJIT implementation is different from the regular JIT. When methods are called multiple times, say 10000 times, MJIT chooses such methods that can be compiled to native code and places them in a queue. MJIT then fetches the queue and converts them to the original code.
Memory optimization
Ruby 3 comes with an advanced garbage collector. It has python’s caching-like API for better memory usage. Since Ruby 1.8, Ruby has steadily evolved in garbage collection algorithms.
Automatic Garbage Compaction
The latest change in garbage collection is Compress it. It was introduced in Ruby 2.7, but as of v2.7 the process is a bit manual. But in version 3 it is completely automatic. Compression is cleverly invoked to ensure proper memory usage.
Objects Grouping
Machines forcing garbage move objects in a package. It groups scattered objects together in a single location in memory so that this memory can be used by heavier objects later.
Parallelism and Concurrency in Ruby
As everyone knows multithreading, parallelism, and at the same time, is one of the most important aspects of any programming language, and this time Ruby developers have improved. That being said, maybe this is one of the factors that makes this version surprisingly faster.
Other Ruby 3 features and changes :
Over the 7 year period, the Ruby community has seen significant improvements in performance and other aspects. In addition to its main goals, Ruby 3 is an interesting update with lots of new features, handy syntax changes, and new improvements. In this section, we will discuss some notable features. One-line pattern matching syntax change
One-line pattern matching syntax change
Matches a one-line pattern previously used in
. Now it is replaced with =>
.
example:
ruby 2.7
1 2 | <span class="token punctuation">{</span> name <span class="token punctuation">:</span> <span class="token string">'John'</span> <span class="token punctuation">,</span> role <span class="token punctuation">:</span> <span class="token string">'CTO'</span> <span class="token punctuation">}</span> <span class="token keyword">in</span> <span class="token punctuation">{</span> name <span class="token punctuation">:</span> <span class="token punctuation">}</span> |
ruby 3.0
1 2 | <span class="token punctuation">{</span> name <span class="token punctuation">:</span> <span class="token string">'John'</span> <span class="token punctuation">,</span> role <span class="token punctuation">:</span> <span class="token string">'CTO'</span> <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token operator">></span> <span class="token punctuation">{</span> name <span class="token punctuation">:</span> <span class="token punctuation">}</span> |
Endless Method definition:
Ruby 3.0 adds endless method definitions. It allows us to create method definitions without the end keyword. According to the developer announced, this will 1 feature that will be tested with this version:
1 2 3 4 5 6 7 | <span class="token comment"># endless method definition</span> <span class="token operator">></span> <span class="token operator">></span> <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">raise_to_power</span></span> <span class="token punctuation">(</span> number <span class="token punctuation">,</span> power <span class="token punctuation">)</span> <span class="token operator">=</span> number <span class="token operator">*</span> <span class="token operator">*</span> power <span class="token operator">></span> <span class="token operator">></span> raise_to_power <span class="token punctuation">(</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token operator">></span> <span class="token number">32</span> |
Except for the method in Hash:
Ruby 3 adds a new method, except
, to the Hash class. The # except hash returns a hash function that excludes the given keys and their values.
Let’s say we need to print or write everything down at some point except for some sensitive data. Specifically, we want to print user details in the log but not the password, we can look through an example as follows:
1 2 3 4 5 6 7 8 | irb <span class="token punctuation">(</span> main <span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token number">001</span> <span class="token punctuation">:</span> <span class="token number">0</span> <span class="token operator">></span> user_details <span class="token operator">=</span> <span class="token punctuation">{</span> name <span class="token punctuation">:</span> <span class="token string">'Akhil'</span> <span class="token punctuation">,</span> age <span class="token punctuation">:</span> <span class="token number">25</span> <span class="token punctuation">,</span> address <span class="token punctuation">:</span> <span class="token string">'India'</span> <span class="token punctuation">,</span> password <span class="token punctuation">:</span> <span class="token string">'T:%g6R'</span> <span class="token punctuation">}</span> irb <span class="token punctuation">(</span> main <span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token number">002</span> <span class="token punctuation">:</span> <span class="token number">0</span> <span class="token operator">></span> puts user_details <span class="token punctuation">.</span> except <span class="token punctuation">(</span> <span class="token symbol">:password</span> <span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token operator">></span> <span class="token punctuation">{</span> name <span class="token punctuation">:</span> <span class="token string">'Akhil'</span> <span class="token punctuation">,</span> age <span class="token punctuation">:</span> <span class="token number">25</span> <span class="token punctuation">,</span> address <span class="token punctuation">:</span> <span class="token string">'India'</span> <span class="token punctuation">}</span> irb <span class="token punctuation">(</span> main <span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token number">003</span> <span class="token punctuation">:</span> <span class="token number">0</span> <span class="token operator">></span> db_info <span class="token operator">=</span> <span class="token constant">YAML</span> <span class="token punctuation">.</span> safe_load <span class="token punctuation">(</span> <span class="token builtin">File</span> <span class="token punctuation">.</span> read <span class="token punctuation">(</span> <span class="token string">'./database.yml'</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> irb <span class="token punctuation">(</span> main <span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token number">004</span> <span class="token punctuation">:</span> <span class="token number">0</span> <span class="token operator">></span> puts db_info <span class="token punctuation">.</span> except <span class="token punctuation">(</span> <span class="token symbol">:username</span> <span class="token punctuation">,</span> <span class="token symbol">:password</span> <span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token operator">></span> <span class="token punctuation">{</span> port <span class="token punctuation">:</span> <span class="token number">5432</span> <span class="token punctuation">,</span> database_name <span class="token punctuation">:</span> <span class="token string">'example_db_production'</span> <span class="token punctuation">}</span> |
NOTE :
Above are some of the notable changes and updates of this ruby version, there are many amazing changes, not to mention its performance, these may be the features that home Publicly provided, according to the information I have found to know, in addition to the above features included in the accompanying update there may be features in the testing process that will be included and More experimental features will be announced soon. Another question is whether ruby upgrading to a new version will have any effect on previous libraries? And the answer is NO why, with the release of ruby 3 the core libraries were modified to fit the goal of ruby, and this doesn’t mean our old apps will break abruptly stopped working. Ruby 3 still ensures that these changes are interoperable, all ready to use new features and new performance improvements and improvements.
Conclusion :
With huge improvements in performance, memory usability, static analysis, and new features, developers have a lot of confidence in Ruby’s future. With Ruby 3, applications can be more scalable and work more enjoyable. The year 2021 is coming not only a new year, but also a new era for all Rubyists.