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

What's New in Ruby 2.4

What's New in Ruby 2.4

Talk given to the Dallas Ruby Brigade on Tuesday, February 7.

Avatar for Steve Crozier

Steve Crozier

February 07, 2017

Other Decks in Programming

Transcript

  1. binding.irb • Quick way to explore environment at a point

    • Uses the mysterious Kernel#binding object (Binding) • Changes you make persist back to program binding.rb
  2. Unified Integers - Fixnum and Bignum are now Integer #

    Before Ruby 2.4 1.class #=> Fixnum (2 ** 62).class #=> Bignum # Ruby 2.4 1.class #=> Integer (2 ** 62).class #=> Integer
  3. Unified Integers - Fixnum and Bignum are now Integer •

    Fixnum and Bignum are deprecated in 2.4 • Writing a gem that might have to deal with both? – Check 1.class – Pre-2.4, you will get Fixnum – 2.4, you will get Integer
  4. New Integer#digits method Want to operate over the digits of

    an integer? Pre-2.4 123.to_s.chars.map(&:to_i).reverse # => [3, 2, 1] 2.4 123.digits # => [3, 2, 1] 123.digits.reverse # => [1, 2, 3]
  5. Precision for float modifiers PI = 3.141592653589793 Pre-2.4 PI.floor #

    => 3 (Fixnum) 2.4 PI.floor # => 3 (Integer) PI.floor(5) # => 3.14159 Applies to ceil, floor, truncate, round (always applied to round)
  6. Precision for float modifiers NUM = 123456789 NUM.round(3) # =>

    123456789.0 (Float) NUM.round(-3) # => 123457000 (Integer) NUM = 12345.6789 NUM.round(-2) # => 12300 (Integer) NUM.round(0) # => 12346 (Integer) NUM.round(-10) # => 0 (Integer)
  7. Float#round options PI = 3.1415 PI.round(3, half: :up) => 3.142

    PI.round(3, half: :down) => 3.141 PI.round(3, half: :even) => 3.142
  8. Symbol#match now works like String#match # 2.3 :hello_ruby.match(/ruby/) #=> 6

    # 2.4 :hello_ruby.match(/ruby/) #=> #<MatchData "ruby"> 'hello_ruby'.match(/ruby/) #=> #<MatchData "ruby">
  9. IO#gets and other methods get a chomp flag So you

    want to remove newline characters from your input? # In 2.3, you did this foo = gets.chomp # 2.4 foo = gets(chomp: true) Works for #gets, #readline, #each_line, #readlines and IO.foreach.
  10. Regexp#named_captures pattern = /(?<first_name>John) (?<last_name>\w+)/ pattern.named_captures => {"first_name"=>[1], "last_name"=>[2]} pattern.match('John

    Brown') => #<MatchData "John Brown" first_name:"John"last_name:"Brown"> New in 2.4 captures = pattern.match('John Brown').named_captures => {"first_name"=>"John", "last_name"=>"Brown"} captures['first_name'] => "John” captures.values_at(:last_name) => ‘Brown’
  11. Enumerable#sum [1, 1, 2, 3, 5, 8, 13, 21].sum #

    => 54 • Starts with an implied (integer) zero • You can provide a starting parameter [1, 1, 2, 3, 5, 8, 13, 21].sum(100) # => 154
  12. Enumerable#sum You can sum anything with method ‘+’ defined… 'Steve'

    + 'Crozier' => "SteveCrozier” ['Steve', 'Crozier'].sum => TypeError: String can't be coerced into Integer …but you must provide a starting value, if not an Integer ['Steve', 'Crozier'].sum('') => "SteveCrozier"
  13. #empty? method was added to Dir, File and Pathname Dir.empty?('path/to/some/dir')

    File.empty?('path/to/some/file') # => same as File.zero? Pathname.new('file-or-dir').empty?
  14. Regexp#match? This new method returns true or false without updating

    the $~ global variable. Since it doesn’t create a MatchData object or update $~, it performs better than #match. /Steve/.match(‘Steve’) # => slower /Steve/.match?(‘Steve’) # => faster