BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Preview of New Features in Ruby 2.3.0

| by Sergio De Simone on Nov 18, 2015. Estimated reading time: 2 minutes |

Nearly one year after Ruby 2.2.0 release, the first preview of Ruby 2.3.0 has been announced. Ruby 2.3.0 Preview1 introduces new features such as immutable string literals, null coalescing operator, and more.

  • immutable string literals: string literals can be now frozen, i.e. marked as immutable, through the following syntax: CONSTANT_STRING = 'constant string'.freeze This will make the developer intent clearer and also provide a significant performance benefit, according to Ruby developer Maciej Mensfeld's benchmarks. If you want to make all string literals be frozen by default, you can use the following "magic" comment:
    # frozen_string_literal: true
  • Safe navigation operator: this will make it easier to deal with nil handling, similarly to what is possible in other languages, such as C#, Swift, etc. Thus, instead of explicitly checking for nil:

    u = User.find(id)
    if u && u.profile && ...
    

    It will be possible to use the new &. null coalescing. This will make it easier to deal with nil handling, similarly to what is possible in other languages, such as C#, Swift, etc. Thus, instead of explicitly checking for nil:

    u = User.find(id)
    if u && u.profile && ...
    

    It will be possible to use the new &. coalescing operator for objects:

    u = User.find(id)
    u&.profile...
    

    According to Matz, Ruby's creator, the &. syntax has the advantage of recalling the expression u && u.profile, which should make it more easily understandable, and of not being confused with ?, which is often used in Ruby as a suffix to method names. ) operator for objects:

    u = User.find(id)
    u&.profile...
    

    According to Matz, Ruby's creator, the &. syntax has the advantage of recalling the expression u && u.profile, which should make it more easily understandable, and of not being confused with ?, which is often used in Ruby as a suffix to method names.

    Similar to the &. operator is the new #dig method added to arrays and hashes. It will allow to access deeply nested data in a safe way:

    myHash.dig(:key1, :key2, :key3)
    

    #dig will return nil if no element is found at the requested location.

  • did_you_mean gem integrated in the language: this will help developers catch misspellings in their code by suggesting them possible corrections for them.

Ruby 2.3.0 includes many more new features, such as deep hash comparison, which will allow to check not only whether two hashes contain the same keys, but also the same values, and will make it possible to compare two hashes as if they were numbers; a new #grep_v function for enumerable objects which will return all elements that do not match a given regex. You can find an exhaustive list in the release notes.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and dont miss out on content that matters to you

BT