Today I Learned

TIL, 2019-09-15, Ruby 2.6

What’s new in Ruby 2.6?

Reference

  • JIT Compiler.
  • Endless range
ary[1..]                            # identical to ary[1..-1]
(1..).each {|index| ... }           # infinite loop from index 1
ary.zip(1..) {|elem, index| ... }   # ary.each.with_index(1) { }
  • Array#union and Array#difference
[1, 1, 2, 2, 3, 3, 4, 5 ].difference([1, 2, 4]) #=> [ 3, 3, 5 ]
["a", "b", "c"].union(["c", "d", "a"])          #=> [ "a", "b", "c", "d" ]
["a"].union([["e", "b"], ["a", "c", "b"]])      #=> [ "a", "e", "b", "c" ]
  • Array #filter is now the alias for Array#select
  • Syntactic sugar to create a hash from an array: (1..5).to_h { |x| [x, x ** 2] } #=> {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}
  • Merging multiple in an array: hash1.merge(hash2, hash3)
  • then, in conjunction with Ruby 2.5’s yield_self.
"https://api.github.com/repos/rails/rails"
  .yield_self { |url| URI.parse(url) }
  .yield_self { |url| Net::HTTP.get(url) }
  .yield_self { |response| JSON.parse(response) }
  .yield_self { |repo| repo.fetch("stargazers_count") }
  .yield_self { |stargazers| "Rails has #{stargazers} stargazers" }
  .yield_self { |string| puts string }
Event.upcoming
  .then { |events| params[:limit] ? events.limit(params[:limit]) : events }
  .then { |events| params[:status] ? events.where(status: status) : events }
  • Random.bytes.
  • We can cover case statements with an else at the end now.

This project is maintained by daryllxd