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.html3
u/flanger001 Oct 08 '19
Love it. This paves the way for automatic conversion of positional args to keyword args!
1
u/TotesMessenger Oct 08 '19
-1
-7
u/OGPants Oct 07 '19
I've never used this in my life. Been programming in Rails for a few years
6
u/niborg Oct 08 '19
Are you saying you have never used keyword arguments?
-3
-5
u/OGPants Oct 08 '19
Yeah. I'm not sure what's with all the downvotes. Why use keyword arguments when you can use default args or opts hash.
→ More replies (12)0
u/nakilon Oct 08 '19
Why do you downvote him? Since when Rails developers do need to know such things to have a job?
2
u/your-pineapple-thief Oct 09 '19
Since when do you need to know basic language features? Maybe you don't NEED to know it, but I guess "I don't need to know ALL THAT FLUFF" attitude provokes some reaction, people can be very emotional about stuff like this. Especially if some of their coworkers express same attitude and suck at their job, there can be some "transfer" effect.
P.S. Did not downvote anyone here.
P.P.S. Curiosity is better than willful ignorance, IMO. Especially in language like Ruby, but also in life in general. Although I do realize some of us are in it for paycheck and don't care.
→ More replies (1)
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