Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Safe Navigation in Ruby 2.3

Rick Liu
January 22, 2016

Safe Navigation in Ruby 2.3

Introduction the new operator and its problem.

Rick Liu

January 22, 2016

Transcript

3 slides
  1. Safe Navigation > some_array.first.positive? > [].first.positive? (value = some_array.first) &&

    value.positive? > some_array.first&.positive? # => true # => NoMethodError: undefined method `positive?' for nil:NilClass # => nil
  2. Safe Navigation if user.account && user.account.trial?
 # process trial account

    settings
 end if user.account&.trial?
 # process trial account settings
 end if user.account && user.account != previous_account
 # setup new account
 end if user.account &.!= previous_account
 # setup new account
 end &.&, &.<<, and &.+

More Decks by Rick Liu

Other Decks in Programming