Ruby On Rails

5 min

Ruby 3.0: Faster Than Before

Ruby 3.0: Faster Than Before

Agnesh ( ROR Developer )

Sachin Gevariya

Founder and Technical Director

Side-by-side comparison showing Rails CSV export memory usage: naive in-memory approach consuming 76 MB+ versus streaming approach holding steady at 1 MB for 1 million rows

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);">&gt;</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);">&gt;</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);">&gt;</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);">&gt;</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);">&gt;</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);">&gt;</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);">&gt;</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);">&gt;</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);">&gt;</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?

  1. The first type profiler will check the new instance of Test which calls test().

  2. Now, test() is bound to value 15 which is an integer.

  3. So, it can be said that a is an integer, meaning the test method takes integer input.

  4. Now, a has an addition (+) operation with 2 (again integer) which will return an integer. So, b is an integer.

  5. 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);">&gt;</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);">&gt;</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);">&gt;</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);">&gt;&gt;</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);">=&gt;</span> a
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;&gt;</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);">&gt;&gt;</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);">=&gt;</span> a
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;&gt;</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);">&gt;&gt;</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);">=&gt;</span> a
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;&gt;</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);">&gt;&gt;</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);">&gt;&gt;</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);">&gt;&gt;</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);">&gt;&gt;</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);">&gt;&gt;</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);">&gt;&gt;</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);">&gt;&gt;</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);">&gt;&gt;</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 =&gt; 2, :c =&gt; 3}</span>
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;&gt;</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);">&gt;&gt;</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 =&gt; 2, :c =&gt; 3}</span>
<span class="token operator" style="box-sizing: border-box; color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">&gt;&gt;</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);">&gt;&gt;</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 =&gt; 2, :c =&gt; 3}</span>

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.

essence logo

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.

essence logo

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.

essence logo