BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Preview of New Features in Ruby 2.3.0

Preview of New Features in Ruby 2.3.0

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

BT