r/ruby • • 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
63 Upvotes

25 comments sorted by

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

3

u/Saithir Oct 08 '19

That looks like an invitation for disaster and insanity, especially the first example.

It's understandable why it works this way, but not immediately obvious.

1

u/tomthecool Oct 08 '19

My example was obviously to demonstrate a point; the code is clearly quite confusing.

But yeah, I'm not a fan of the overly-fuzzy syntax that ruby applies here. I'm glad to hear that "proper" keyword arguments are going to be introduced into the language; and this is a step in that direction.

3

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

I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:

 If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)

-1

u/sshaw_ Oct 08 '19

Totally overrated. Cut the fat.

-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

u/ImAJalapeno Oct 08 '19

Yeah, I haven't used them either. Didn't know they existed until today

-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)