Upgrade to ruby 3.0.5

Ruby 3.0 came with a few breaking changes which would cause much hassle for a typical rails app that uses a lot of gems, especially the keyword argument behavior breaking change and the removal of the URI.escape method.

Keyword argument behavior change

  • before ruby 3
irb(main):001:0> RUBY_VERSION
=> "2.7.6"
irb(main):002:1* def add(a: 1, b: 2)
irb(main):003:1*   sum = a + b
irb(main):004:1*   puts "#{a} + #{b} = #{sum}"
irb(main):005:0> end
=> :add
irb(main):006:0> add({a: 10, b: 20})
10 + 20 = 30
=> nil
  • after ruby 3
irb(main):001:0> RUBY_VERSION
=> "3.0.5"
irb(main):002:1* def add(a: 1, b: 2)
irb(main):003:1*   sum = a + b
irb(main):004:1*   puts "#{a} + #{b} = #{sum}"
irb(main):005:0> end
=> :add
irb(main):006:0> add({a: 10, b: 20})
# (irb):2:in `add': wrong number of arguments (given 1, expected 0) (ArgumentError)
#         from (irb):6:in `<main>'
#         from /Users/andy/.rbenv/versions/3.0.5/lib/ruby/gems/3.0.0/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
#         from /Users/andy/.rbenv/versions/3.0.5/bin/irb:23:in `load'
#         from /Users/andy/.rbenv/versions/3.0.5/bin/irb:23:in `<main>'
irb(main):007:0> add(**{a: 10, b: 20})
10 + 20 = 30
=> nil

No URI.escape method and solution

irb(main):001:0> RUBY_VERSION
=> "3.0.5"
irb(main):002:0> require 'uri'
=> true
irb(main):003:0> url = 'http://www.google.com/?q=ruby 3'
=> "http://www.google.com/?q=ruby 3"
irb(main):004:0> URI.escape url
# (irb):4:in `<main>': undefined method `escape' for URI:Module (NoMethodError)
#         from /Users/andy/.rbenv/versions/3.0.5/lib/ruby/gems/3.0.0/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
#         from /Users/andy/.rbenv/versions/3.0.5/bin/irb:23:in `load'
#         from /Users/andy/.rbenv/versions/3.0.5/bin/irb:23:in `<main>'
irb(main):005:0> URI.encode_www_form_component url
=> "http%3A%2F%2Fwww.google.com%2F%3Fq%3Druby+3"
irb(main):006:0> require 'cgi'
=> true
irb(main):007:0> CGI.escape url
=> "http%3A%2F%2Fwww.google.com%2F%3Fq%3Druby+3"

References

ruby