BT

InfoQ Homepage News Preview of New Features in Ruby 2.3.0

Preview of New Features in Ruby 2.3.0

Bookmarks

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
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.

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

Community comments

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

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

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.