Skip to content

Instantly share code, notes, and snippets.

@ang3lkar
Last active September 9, 2019 10:30
Show Gist options
  • Save ang3lkar/89e5e5d84a748ef305ce715df4d007e6 to your computer and use it in GitHub Desktop.
Save ang3lkar/89e5e5d84a748ef305ce715df4d007e6 to your computer and use it in GitHub Desktop.
Rails Upgrade

Ruby Upgrade

Dear people, as you may already know we transcend to the Rails5 era! Rails 5 required a Ruby upgrade so, apart from the new Rails APIs, we jumped from Ruby 2.3.8 to 2.6.3!

Apart from many performance improvements with less memory consumption, this gives us several goodies we can use from now on.

How to upgrade

brew install libidn
rvm install ruby-2.6.3
sudo gem update --system 2.6.3
gem install bundler:2.0.2
bundle

What do I get?

GitHub upgraded to Ruby 2.6 and saw a 3% decrease in post boot memory usage. Direct instruction marking in Ruby 2.6 - The GitHub Blog

  • Enumerable chain
e = (1..3).chain([4, 5])
e.to_a #=> [1, 2, 3, 4, 5]
  • We can use yield_self or then!
construct_url
  .yield_self { |url| Faraday.get(url) }.body
  .yield_self { |response| JSON.parse(response) }
  .dig('object', 'id')
  .then { |id| id || '<undefined>' }
  .then { |id| "server:#{id}" }
  • Support pattern in any?, all? & none?. It lets us do away with the syntactic noise incurred by opening a block.
strs.any? { |str| /aeiou/i }
strs.all? { |str| /aeiou/i }
nums.none? { |num| /aeiou/i }
  • Endless ranges. There was no simple way in Ruby to write an endless loop with index.
# examples
ary[1..]                          # identical to ary[1..-1] without magical -1
(1..).each {|index| ... }         # enumerates values starting from index 1
ary.zip(1..) {|elem, index| ... } # ary.each.with_index(1) { ... }
  • Function composition operators
f = proc{|x| x + 2}
g = proc{|x| x * 3}
(f << g).call(3) # -> 11; identical to f(g(3))
(f >> g).call(3) # -> 15; identical to g(f(3))

You can find a more elaborate analysis here:

#workable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment