
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.
Faster Ruby:
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.
Concurrent Ruby:
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.
Correct Ruby:
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.
Example:
<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Test</span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">test</span></span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">></span>void <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#test() returns nothing</span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">to_s</span></span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">></span> <span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">String</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">|</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">Integer</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">></span> <span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">String</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#to_s() takes null or integer as an argument and returns string</span> <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span
<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Test</span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">test</span></span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">></span>void <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#test() returns nothing</span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">to_s</span></span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">></span> <span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">String</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">|</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">Integer</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">></span> <span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">String</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#to_s() takes null or integer as an argument and returns string</span> <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span
<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Test</span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">test</span></span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">></span>void <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#test() returns nothing</span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">to_s</span></span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">></span> <span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">String</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">|</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">Integer</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">></span> <span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">String</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#to_s() takes null or integer as an argument and returns string</span> <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span
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.
Example:
<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Test</span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">test</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span>a<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> b <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> a<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">+</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">2</span> <span class="token keyword keyword-return" style="box-sizing: border-box; color: rgb(0, 119, 170);">return</span> b <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span> <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Test</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span><span class="token keyword keyword-new" style="box-sizing: border-box; color: rgb(0, 119, 170);">new</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>test<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">15</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span
<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Test</span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">test</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span>a<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> b <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> a<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">+</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">2</span> <span class="token keyword keyword-return" style="box-sizing: border-box; color: rgb(0, 119, 170);">return</span> b <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span> <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Test</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span><span class="token keyword keyword-new" style="box-sizing: border-box; color: rgb(0, 119, 170);">new</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>test<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">15</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span
<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Test</span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">test</span></span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span>a<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> b <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> a<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">+</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">2</span> <span class="token keyword keyword-return" style="box-sizing: border-box; color: rgb(0, 119, 170);">return</span> b <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span> <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Test</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span><span class="token keyword keyword-new" style="box-sizing: border-box; color: rgb(0, 119, 170);">new</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>test<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">15</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span
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:
<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Test</span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">test</span></span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">Integer</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">></span> <span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">Integer</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#test passes int as an argument and returns int.</span> <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span
<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Test</span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">test</span></span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">Integer</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">></span> <span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">Integer</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#test passes int as an argument and returns int.</span> <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span
<span class="token keyword keyword-class" style="box-sizing: border-box; color: rgb(0, 119, 170);">class</span> <span class="token class-name" style="box-sizing: border-box; color: rgb(221, 74, 104);">Test</span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">test</span></span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">Integer</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">></span> <span class="token builtin" style="box-sizing: border-box; color: rgb(102, 153, 0);">Integer</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#test passes int as an argument and returns int.</span> <span class="token keyword keyword-end" style="box-sizing: border-box; color: rgb(0, 119, 170);">end</span
What’s new:
‘ => ‘ symbol is added which can be used as rightward assignment operator(right assignment)
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">1</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=></span> a <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> puts a <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#prints 1</span>
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">1</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=></span> a <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> puts a <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#prints 1</span>
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">1</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=></span> a <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> puts a <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#prints 1</span>
Now ‘in’ returns true or false
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> <span class="token constant" style="box-sizing: border-box; color: rgb(153, 0, 85);">O</span> <span class="token keyword keyword-in" style="box-sizing: border-box; color: rgb(0, 119, 170);">in</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">1</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#returns false in the latest version, earlier it was giving an error.</span>
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> <span class="token constant" style="box-sizing: border-box; color: rgb(153, 0, 85);">O</span> <span class="token keyword keyword-in" style="box-sizing: border-box; color: rgb(0, 119, 170);">in</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">1</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#returns false in the latest version, earlier it was giving an error.</span>
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> <span class="token constant" style="box-sizing: border-box; color: rgb(153, 0, 85);">O</span> <span class="token keyword keyword-in" style="box-sizing: border-box; color: rgb(0, 119, 170);">in</span> <span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">1</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#returns false in the latest version, earlier it was giving an error.</span>
The inline method definition is added.
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">square</span></span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span>n<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> n<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">*</span>n
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">square</span></span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span>n<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> n<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">*</span>n
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> <span class="token keyword keyword-def" style="box-sizing: border-box; color: rgb(0, 119, 170);">def</span> <span class="token method-definition" style="box-sizing: border-box;"><span class="token function" style="box-sizing: border-box; color: rgb(221, 74, 104);">square</span></span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span>n<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> n<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">*</span>n
except() function is added for hash
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> h <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">a</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">1</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">b</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">2</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">c</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">3</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">}</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> puts h<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>except<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:a</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#prints {:b => 2, :c => 3}</span>
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> h <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">a</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">1</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">b</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">2</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">c</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">3</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">}</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> puts h<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>except<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:a</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#prints {:b => 2, :c => 3}</span>
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> h <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">{</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">a</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">1</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">b</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">2</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">,</span> <span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">c</span><span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">:</span><span class="token number" style="box-sizing: border-box; color: rgb(153, 0, 85);">3</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">}</span> <span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">>></span> puts h<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">.</span>except<span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">(</span><span class="token symbol" style="box-sizing: border-box; color: rgb(153, 0, 85);">:a</span><span class="token punctuation" style="box-sizing: border-box; color: rgb(153, 153, 153);">)</span> <span class="token comment" style="box-sizing: border-box; color: rgb(112, 128, 144);">#prints {:b => 2, :c => 3}</span>
Reader guide
Explore this topic
Explore this topic
Use the links below to jump into next reading without leaving the article flow.
Blog categories
Recent posts
Rails CSV Export: Why Large Data Exports Fail and How to Build Them Properly
7 min
Rails Background Jobs: Why Customers Shouldn’t Wait for Work That Can Run Later
6 min
Multi-Tenant Rails: 4 Data Isolation Problems SaaS Teams Need to Prevent
5 min
What Breaks When an AI-Generated MVP Goes to Production
5 min
Similar Blogs
Explore Similar Blogs
Explore Similar Blogs
Learn how AI is transforming meetings, notes,
and team productivity.
Learn how AI is transforming meetings, notes,
and team productivity.

next step
Tell us what you are trying to build.
No spec required. Send a paragraph, an old repo link, or a screenshot of the system you are replacing. A senior Rails engineer will review it and reply with real feedback.




next step
Tell us what you are trying to build.
No spec required. Send a paragraph, an old repo link, or a screenshot of the system you are replacing. A senior Rails engineer will review it and reply with real feedback.




next step
Tell us what you are trying to build.
No spec required. Send a paragraph, an old repo link, or a screenshot of the system you are replacing. A senior Rails engineer will review it and reply with real feedback.






