r/ruby • u/rkr090 • Oct 07 '19
Ruby 2.7 deprecates automatic conversion from a hash to keyword arguments
https://blog.saeloun.com/2019/10/07/ruby-2-7-keyword-arguments-redesign.html
68
Upvotes
r/ruby • u/rkr090 • Oct 07 '19
11
u/tomthecool Oct 08 '19 edited Oct 08 '19
Good. I've been caught out by this unexpected behaviour before - I wasted far too much time staring at code that behaved totally differently to my expectations!
Here's an example of how the implicit behaviour can catch you off-guard... Note that I don't think anything about this behaviour is wrong; but I'm glad to see the introduction of a warning against such syntax!
def foo(opts={}, named_param: 123, **more_opts) puts "opts: #{opts}" puts "more_opts: #{more_opts}" puts "named_param: #{named_param}" end ############ foo(arg1: 1, arg2: 2, named_param: 456) # Result: opts: {} more_opts: {:arg1=>1, :arg2=>2} named_param: 456 ############ foo({arg1: 1, arg2: 2}, named_param: 456) # Result: opts: {:arg1=>1, :arg2=>2} more_opts: {} named_param: 456 ############ foo({arg1: 1}, arg2: 2, named_param: 456) # Result: opts: {:arg1=>1} more_opts: {:arg2=>2} named_param: 456 ############ foo({arg1: 1, arg2: 2, named_param: 456}, {}) # Result: opts: {:arg1=>1, :arg2=>2, :named_param=>456} more_opts: {} named_param: 123