New features in Ruby 2.4

Post on 11-Apr-2017

245 views 1 download

Transcript of New features in Ruby 2.4

New Features in Ruby 2.4

Ireneusz SkrobiśLead Developer @ Selleo

New #sum method for Enumerable

New #sum method for Enumerable

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

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.

Faster Array#min and Array#max

Faster Array#min and Array#max

Array#min: 35.1 i/s

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

Extract named captures from Regexp match results

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"}

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"]

Faster regular expressions with Regexp#match?

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

New methods for testing if directories/files are empty

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

New Integer#digits method

New Integer#digits method

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

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]

Moves configuration to Logger’s constructor

Moves configuration to Logger’s constructor

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

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'

Simplified integers

Simplified integers

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

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]

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]

New arguments supported for float modifiers

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

You can find more information here

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

Thank you!Live long and prosper :)

Ireneusz SkrobiśLead Developer @ Selleo