What's new in Ruby 2.3.0

fetching an inverse pattern

friends = %w[John Alain Jim Delmer]

j_friends = friends.grep(/^J/)
#=> ["John", "Alain", "Jim", "Delmer"] 

j_friends = friends.grep_v(/^J/)
#=> ["Alain", "Delmer"] 


dig a hash

user = {
  user: {
    address: {
      street1: '123 Main street'
    }
  }
}

user.dig(:user, :address, :street1) # => '123 Main street'

dig an array

results = [[[1, 2, 3]]]

results.dig(0, 0, 0) # => 1

fetch multiple values from a hash

values = {
  foo: 1,
  bar: 2,
  baz: 3,
  qux: 4
}

values.values_at(:foo, :bar)    # => [1, 2]
values.fetch_values(:foo, :bar) # => [1, 2]

helpful error messages

1.postive?
NoMethodError: undefined method `postive?' for 1:Fixnum
Did you mean?  positive?

Numeric now has methods positive? and negative?

1.positive? #=> true

Introducing the safe naviation operator

Prevent further execution of a chained method if the method doesn't exist

class Robot
  def walk
    puts "I'm walking"
  end
end

robot = Robot.new
robot.walk

robot.talk
#=> NoMethodError: undefined method `talk' for #<Robot:0x0000000219c150></Robot:0x0000000219c150>

robot&.talk
#=> NoMethodError: undefined method `talk' for #<Robot:0x0000000219c150></Robot:0x0000000219c150>

robot
#=> #<Robot:0x0000000219c150></Robot:0x0000000219c150> 

Note: The safe navigation operator doesn't seem to be fully tested yet as I can't seem to get the nil result I was expecting. I'm using 2.3.0-preview1.

Resources