I came across this on r/ruby. Here's a brief explanation via some pry snippets:

  • && is a short-circuit operator in Ruby.

    > 1 && 2 && 3
    => 3
    # last value gets returned till it hits a nil
    > 1 && nil && nil.strip
    => nil
    # nil.strip was not evaluated 
    
  • a &&= b is the same as a = a && b

> options = {}
=> {}

Given the options hash, we have two scenarios:

  • Case 1: options contains key :name
> options[:name] = "foo  "
=> "foo "
> options[:name] && options[:name].strip
# <=> "foo " && "foo ".strip
# <=> "foo ".strip
=> "foo"
  • Case 2: options does not contain key :name
> options[:name]
=> nil
> options[:name] && options[:name].strip
# <=> nil && nil.strip
=> nil

#explainlikeiam5 #reddit