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

Ruby 2.7 - Beyond the CHANGELOG

Ruby 2.7 - Beyond the CHANGELOG

Let's go beyond the CHANGELOG and answer the important questions. In this talk I will give you some insights what ruby 2.7 is about, what kinds of problems you are going to find ahead and answer the ultimate question: Is it worth upgrading?

Gabriel Mazetto

January 14, 2020
Tweet

More Decks by Gabriel Mazetto

Other Decks in Programming

Transcript

  1. 1

    View Slide

  2. @brodock
    Gabriel Mazetto
    2
    ɡ a b ɾ i ˈ ɛ w m a z ˈ ɛ t t u

    View Slide

  3. 3

    View Slide

  4. 4

    View Slide

  5. 5

    View Slide

  6. 6

    View Slide

  7. 7

    View Slide

  8. 8

    View Slide

  9. 9

    View Slide

  10. 10

    View Slide

  11. 11

    View Slide

  12. https://ruby-lang.org

    View Slide

  13. 13

    View Slide

  14. 14
    Highlights

    View Slide

  15. 15
    [experimental]

    View Slide

  16. irb(main):001:0> [1,2,3] in [a,b,c]
    irb(main):002:0> c
    => 3
    16

    View Slide

  17. irb(main):001:0> [1,2,3] in [a,b,c]
    (irb):1: warning: Pattern matching is experimental, and
    the behavior may change in future versions of Ruby!
    => nil
    irb(main):002:0> c
    => 3
    17

    View Slide

  18. 18

    View Slide

  19. 19

    View Slide

  20. GC.compact
    20

    View Slide

  21. 21

    View Slide

  22. def something(args = {})
    # ...
    end
    args = {a: 1, b: 2}
    something(args)
    22

    View Slide

  23. def something(a:, b:)
    # ...
    end
    args = {a: 1, b: 2}
    something(args)
    23

    View Slide

  24. def something(a:, b:)
    # ...
    end
    args = {a: 1, b: 2}
    something(args)
    warning: Using the last argument as keyword parameters is
    deprecated; maybe ** should be added to the call
    warning: The called method `something' is defined here
    24

    View Slide

  25. def something(a:, b:)
    # ...
    end
    args = {a: 1, b: 2}
    something( **args)
    25

    View Slide

  26. What is the **
    syntax?
    26

    View Slide

  27. def something( **args)
    # args => {a: 1, b: 2}
    end
    something(a: 1, b: 2)
    27

    View Slide

  28. def something(a:, b:)
    # a = 1
    # b = 2
    end
    args = {a: 1, b: 2}
    something( **args)
    28

    View Slide

  29. def something(*args)
    # args = [1, 2, 3]
    end
    something(1,2,3)
    29

    View Slide

  30. def something(a, b, c)
    # a = 1
    # b = 2
    # c = 3
    end
    args = [1,2,3]
    something(*args)
    30

    View Slide

  31. def something(*args, **kwargs)
    # args = [1]
    # kwargs = {b: 2}
    end
    something(1, b: 2)
    31

    View Slide

  32. def something(a, b:)
    end
    args = [1]
    kwargs = {b:2}
    something(*args, **kwargs)
    32

    View Slide

  33. 33

    View Slide

  34. 34

    View Slide

  35. 35

    View Slide

  36. 36
    Lowlights

    View Slide

  37. 37

    View Slide

  38. 38
    # Ruby <= 2.6
    10.times { |value| puts value }
    # Ruby 2.7
    10.times { puts _1 }

    View Slide

  39. 39

    View Slide

  40. # Ruby 2.7
    el = %W(a b c d e f a b a b c d a b)
    el.tally
    # => {"a" =>4, "b" =>4, "c" =>2, "d" =>2, "e" =>1, "f" =>1}
    40

    View Slide

  41. # Ruby <= 2.6 V1
    el.group_by { |v| v }.transform_values(&:size)
    41

    View Slide

  42. # Ruby <= 2.6 V2
    el.group_by { |v| v }.map { |k, v| [k, v.size] }.to_h
    42

    View Slide

  43. # Ruby <= 2.6 V3
    el.group_by { |v| v }.to_h { |k, v| [k, v.size] }
    43

    View Slide

  44. # Ruby <= 2.6 V4
    el.each_with_object(Hash.new(0)) { |v, h| h[v] += 1 }
    44

    View Slide

  45. 45

    View Slide

  46. [1,2,3,4].intersection [2,3]
    # => [2, 3]
    46

    View Slide

  47. 47

    View Slide

  48. enum = 1.upto(1_000)
    enum.filter_map { |i| i + 1 if i.even? }
    # => [3, 5, 7, 9, 11, 13, ...]
    48

    View Slide

  49. 49

    View Slide

  50. CGI.escapeHTML(html)
    EscapeUtils.escape_html(html)
    50

    View Slide

  51. Haml ::Helpers.html_escape(html)
    ERB ::Util.html_escape(html)
    String.gsub(/[&"'><]/, html_escape)
    Rack ::Utils.escape_html(html)
    51

    View Slide

  52. 52
    One interesting story...

    View Slide


  53. 53

    View Slide

  54. New Japanese Era
    54
    Heisei Reiwa

    View Slide

  55. Unicode 12.1:
    令和
    U+32FF ㋿
    (SQUARE ERA NAME REIWA)
    55

    View Slide

  56. "\u4EE4\u548C" == "\u32FF".unicode_normalize(:nfkc)
    56
    令和 ㋿ 令和

    View Slide

  57. Unicode 12.1:
    New Emojis
    57
    ⚕⚕⚖

    View Slide

  58. 58
    The remaining question:
    Is Ruby 2.7 worth upgrading?

    View Slide

  59. 59


    View Slide