Categories
Programming

Ruby 2.7.0

As per tradition, a new version of Ruby was released on Christmas Day. Out of the 4,190 file changes since 2.6.0, Ruby 2.7.0 introduces some notable improvements.

REPL

  • irb now supports multi-line editing:
2.7.0 > def greeting
2.7.0 >   "Hello"
2.7.0 > end
 => :greeting
2.7.0 > greeting
 => "Hello"

Beginless Range

  • An experimental feature that allows you to omit the beginning of a range:
Listing.where(sold_price: ..500_000) 
# Identical to 1..500_000

Call private method with self

  • Allows calling a private method with a literal self as the receiver, making it consistent with calling private attribute assignment methods:
def assessment
end
private :assessment
self.assessment

.tally

  • Counts the occurence of each element; reducing the need for group by statements:
2.7.0 > ['jab', 'jab', 'hook'].tally
 => { 'jab' => 2, 'hook' => 1 }

Enumerator::Lazy#eager

  • Generates non-lazy enumerator from a lazy enumerator:

“This is useful where a normal Enumerable object needs to be generated while lazy operation is still desired to avoid creating intermediate arrays.”

– Yukihiro Matsumoto
2.7.0 > a = %w(foo bar baz)
2.7.0 > e = a.lazy.map {|x| x.upcase }.map {|x| x + "!" }.eager
2.7.0 > p e.class
 => Enumerator
2.7.0 > p e.map {|x| x + "?" }
 => ["FOO!?", "BAR!?", "BAZ!?"]

For the full list of release notes: https://www.ruby-lang.org/en/news/2019/12/25/ruby-2-7-0-released/.

By Adam Naamani

Real estate specialist, software engineer, and writer based in Vancouver, British Columbia.

Leave a Reply