What's new in Ruby 2.3

43
What's new in Ruby 2.3 Simon Courtois — @happynoff

Transcript of What's new in Ruby 2.3

What's new in Ruby 2.3

Simon Courtois — @happynoff

bit.ly/tinci-ruby-23 inci@tincihq

&.

bit.ly/tinci-ruby-23 inci@tincihq

article && article.author && article.author.name

bit.ly/tinci-ruby-23 inci@tincihq

article .try!(:author) .try!(:name)

bit.ly/tinci-ruby-23 inci@tincihq

article&.author&.name

bit.ly/tinci-ruby-23 inci@tincihq

article.try! do |art| # called if article # is not nil end

bit.ly/tinci-ruby-23 inci@tincihq

article&. do |art| # syntax error end

bit.ly/tinci-ruby-23 inci@tincihq

article.try!(:has_tag?, hottest_tag()) # hottest_tag is always called

article&.has_tag?(hottest_tag()) # hottest_tag called only if needed

bit.ly/tinci-ruby-23 inci@tincihq

Hashes comparison

bit.ly/tinci-ruby-23 inci@tincihq

{ a: 1, b: 2, c: 3 } > { a: 1, b: 2 }

bit.ly/tinci-ruby-23 inci@tincihq

{ a: 1, b: 2 } >= { a: 1, b: 2 }

bit.ly/tinci-ruby-23 inci@tincihq

<<~

bit.ly/tinci-ruby-23 inci@tincihq

class Person def say_hello message = <<END Hello,

Delighted to meet you! END puts message end end

bit.ly/tinci-ruby-23 inci@tincihq

>> Hello, >> >> Delighted to meet you!

bit.ly/tinci-ruby-23 inci@tincihq

class Person def say_hello message = <<-END Hello,

Delighted to meet you! END puts message end end

bit.ly/tinci-ruby-23 inci@tincihq

>> Hello, >> >> Delighted to meet you!

bit.ly/tinci-ruby-23 inci@tincihq

class Person def say_hello_again message = <<-END Hello again,

Delighted to meet you! END puts message.strip_heredoc end end

bit.ly/tinci-ruby-23 inci@tincihq

>> Hello, >> >> Delighted to meet you!

bit.ly/tinci-ruby-23 inci@tincihq

class Person def say_hello message = <<~END Hello,

Delighted to meet you! END puts message end end

bit.ly/tinci-ruby-23 inci@tincihq

>> Hello, >> >> Delighted to meet you!

bit.ly/tinci-ruby-23 inci@tincihq

Did you mean?

bit.ly/tinci-ruby-23 inci@tincihq

"hello world".uppcase # undefined method `uppcase' for… # Did you mean? upcase # upcase!

bit.ly/tinci-ruby-23 inci@tincihq

New methods!

bit.ly/tinci-ruby-23 inci@tincihq

Array#dig

bit.ly/tinci-ruby-23 inci@tincihq

arr[1][2][0] # => 4

arr = [1, [2, 3, [4]]]

bit.ly/tinci-ruby-23 inci@tincihq

arr[1][3][0] # undefined method `[]' # for nil

arr = [1, [2, 3, [4]]]

bit.ly/tinci-ruby-23 inci@tincihq

arr[1] && arr[1][3] && arr[1][3][0] # => nil

arr = [1, [2, 3, [4]]]

bit.ly/tinci-ruby-23 inci@tincihq

arr.dig(1, 2, 0) # => 4 arr.dig(1, 3, 0) # => nil

arr = [1, [2, 3, [4]]]

bit.ly/tinci-ruby-23 inci@tincihq

arr.dig(1, 1, 0) # Fixnum does not # have #dig method

arr = [1, [2, 3, [4]]]

bit.ly/tinci-ruby-23 inci@tincihq

Hash#dig

bit.ly/tinci-ruby-23 inci@tincihq

hash = {article: {author: {name: "Bob"}}}

hash.dig(:article, :author, :name) # => "Bob"

hash.dig(:article, :writer, :name) # => nil

bit.ly/tinci-ruby-23 inci@tincihq

hash.dig(:article, :tags, 1) # => "cool"

hash = {article: {tags: ["ruby", "cool"]}}

bit.ly/tinci-ruby-23 inci@tincihq

Hash#fetch_values

bit.ly/tinci-ruby-23 inci@tincihq

hash = { coffee: "black", milk: "white", tomato: "red" }

hash.fetch_values(:coffee, :tomato) # => ["black", "red"]

bit.ly/tinci-ruby-23 inci@tincihq

hash.fetch_values(:coffee, :banana) # key not found: :banana

hash = { coffee: "black", milk: "white", tomato: "red" }

bit.ly/tinci-ruby-23 inci@tincihq

hash.fetch_values(:milk, :banana) { |key| "unknown #{key}" } # => ["white", "unknown banana"]

hash = { coffee: "black", milk: "white", tomato: "red" }

bit.ly/tinci-ruby-23 inci@tincihq

Stay positive

bit.ly/tinci-ruby-23 inci@tincihq

1.positive? # => true -1.positive? # => false

1.negative? # => false -1.negative? # => true

bit.ly/tinci-ruby-23 inci@tincihq

[1, 2, 3].all?(&:positive?) # => true

[1, -2, 3].all?(&:positive?) # => false

bit.ly/tinci-ruby-23 inci@tincihq

bit.ly/ruby-23

inciwww.tinci.fr

Simon Courtois, Software Architect at

Ruby on Rails, .NET, EmberJS

bit.ly/tinci-ruby-23 inci@tincihq

Questions?

bit.ly/tinci-ruby-23 inci@tincihq

Thanks!