New features in Ruby 2.4

27
New Features in Ruby 2.4 Ireneusz Skrobiś Lead Developer @ Selleo

Transcript of New features in Ruby 2.4

Page 1: New features in Ruby 2.4

New Features in Ruby 2.4

Ireneusz SkrobiśLead Developer @ Selleo

Page 2: New features in Ruby 2.4

New #sum method for Enumerable

Page 3: New features in Ruby 2.4

New #sum method for Enumerable

[1, 1, 2, 3, 5, 8, 13, 21].sum # => 54

Page 4: New features in Ruby 2.4

New #sum method for Enumerable

[1, 1, 2, 3, 5, 8, 13, 21].sum # => 54

The #sum method has an optional parameter which defaults to 0.This value is the starting value of a summation meaning that [].sum is 0.

Page 5: New features in Ruby 2.4

Faster Array#min and Array#max

Page 6: New features in Ruby 2.4

Faster Array#min and Array#max

Array#min: 35.1 i/s

Enumerable#min: 21.8 i/s - 1.61x slower

Page 7: New features in Ruby 2.4

Extract named captures from Regexp match results

Page 8: New features in Ruby 2.4

Extract named captures from Regexp match results

pattern = /(?<first_name>John) (?<last_name>\w+)/pattern.match('John Backus').named_captures# => {"first_name" => "John", "last_name" => "Backus"}

Page 9: New features in Ruby 2.4

Extract named captures from Regexp match results

pattern = /(?<first_name>John) (?<last_name>\w+)/pattern.match('John Backus').named_captures# => {"first_name" => "John", "last_name" => "Backus"}

pattern = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/pattern.match('2016-02-01').values_at(:year, :month) # => ["2016", "02"]

Page 10: New features in Ruby 2.4

Faster regular expressions with Regexp#match?

Page 11: New features in Ruby 2.4

Faster regular expressions with Regexp#match?

Regexp#match?: 2630002.5 i/s

Regexp#===: 872217.5 i/s - 3.02x slower

Regexp#=~: 859713.0 i/s - 3.06x slower

Regexp#match: 539361.3 i/s - 4.88x slower

Page 12: New features in Ruby 2.4

New methods for testing if directories/files are empty

Page 13: New features in Ruby 2.4

New methods for testing if directories/files are empty

Dir.empty?('empty_directory') # => true

Dir.empty?('directory_with_files') # => false

File.empty?('contains_text.txt') # => false

File.empty?('empty.txt') # => true

Page 14: New features in Ruby 2.4

New Integer#digits method

Page 15: New features in Ruby 2.4

New Integer#digits method

123.digits # => [3, 2, 1]123.digits[0] # => 3

Page 16: New features in Ruby 2.4

New Integer#digits method

123.digits # => [3, 2, 1]123.digits[0] # => 3

# Equivalent behavior in Ruby 2.3:123.to_s.chars.map(&:to_i).reverse # => [3, 2, 1]

Page 17: New features in Ruby 2.4

Moves configuration to Logger’s constructor

Page 18: New features in Ruby 2.4

Moves configuration to Logger’s constructor

logger = Logger.new(STDOUT, level: :info, progname: 'LOG')

Page 19: New features in Ruby 2.4

Moves configuration to Logger’s constructor

logger = Logger.new(STDOUT, level: :info, progname: 'LOG')

# Equivalent behavior in Ruby 2.3:logger = Logger.new(STDOUT)logger.level = :infologger.progname = 'LOG'

Page 20: New features in Ruby 2.4

Simplified integers

Page 21: New features in Ruby 2.4

Simplified integers

# Find classes which subclass the base "Numeric" class:numerics = ObjectSpace.each_object(Module).select { |mod| mod < Numeric }

Page 22: New features in Ruby 2.4

Simplified integers

# Find classes which subclass the base "Numeric" class:numerics = ObjectSpace.each_object(Module).select { |mod| mod < Numeric }

# In Ruby 2.3:numerics # => [Complex, Rational, Bignum, Float, Fixnum, Integer, BigDecimal]

Page 23: New features in Ruby 2.4

Simplified integers

# Find classes which subclass the base "Numeric" class:numerics = ObjectSpace.each_object(Module).select { |mod| mod < Numeric }

# In Ruby 2.3:numerics # => [Complex, Rational, Bignum, Float, Fixnum, Integer, BigDecimal]

# In Ruby 2.4:numerics # => [Complex, Rational, Float, Integer, BigDecimal]

Page 24: New features in Ruby 2.4

New arguments supported for float modifiers

Page 25: New features in Ruby 2.4

New arguments supported for float modifiers

4.55.ceil(1) # => 4.6

4.55.floor(1) # => 4.5

4.55.truncate(1) # => 4.5

4.55.round(1) # => 4.6

Page 26: New features in Ruby 2.4

You can find more information here

https://blog.blockscore.com/new-features-in-ruby-2-4/

Page 27: New features in Ruby 2.4

Thank you!Live long and prosper :)

Ireneusz SkrobiśLead Developer @ Selleo