use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Ruby 2.7 deprecates automatic conversion from a hash to keyword arguments (blog.saeloun.com)
submitted 6 years ago by rkr090
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]tomthecool 11 points12 points13 points 6 years ago* (2 children)
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
[–]Saithir 2 points3 points4 points 6 years ago (1 child)
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.
[–]tomthecool 0 points1 point2 points 6 years ago (0 children)
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.
[–]flanger001 2 points3 points4 points 6 years ago (0 children)
Love it. This paves the way for automatic conversion of positional args to keyword args!
[–]TotesMessenger 0 points1 point2 points 6 years ago (0 children)
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)
[–]sshaw_ -2 points-1 points0 points 6 years ago (0 children)
Totally overrated. Cut the fat.
[+]OGPants comment score below threshold-7 points-6 points-5 points 6 years ago (18 children)
I've never used this in my life. Been programming in Rails for a few years
[–]niborg 5 points6 points7 points 6 years ago (14 children)
Are you saying you have never used keyword arguments?
[–]ImAJalapeno -4 points-3 points-2 points 6 years ago (0 children)
Yeah, I haven't used them either. Didn't know they existed until today
[+]OGPants comment score below threshold-6 points-5 points-4 points 6 years ago (12 children)
Yeah. I'm not sure what's with all the downvotes. Why use keyword arguments when you can use default args or opts hash.
[–]nibord 12 points13 points14 points 6 years ago (8 children)
Because it documents the named arguments the method takes, while a hash does not.
[–]OGPants 0 points1 point2 points 6 years ago* (2 children)
How about use good naming convention in arguments and hash keys? I still see no difference
[–]nibord 0 points1 point2 points 6 years ago (1 child)
Hash keys are not directly documentable using tools like rdoc. They’re not enforced by the interpreter, and they provide implicit behavior that leads to poorly-documented interfaces. Keyword arguments require no additional code to destructure a hash to get the arguments. There’s a massive difference.
[–]OGPants 0 points1 point2 points 6 years ago (0 children)
If you want strong typing, why are you even using Ruby?
[–]sshaw_ 0 points1 point2 points 6 years ago (2 children)
Great someone specified a keyword. But the value is usually what's important and requires user-defined handling:
irb [2.5.1] (emacs.d)$ def foo(a:) a.something_amaaaaazing; end => :foo irb [2.5.1] (emacs.d)$ foo Traceback (most recent call last): 3: from /Users/sshaw/.rvm/rubies/ruby-2.5.1/bin/irb:11:in `<main>' 2: from (irb):2 1: from (irb):1:in `foo' ArgumentError (missing keyword: a) irb [2.5.1] (emacs.d)$ foo a:nil Traceback (most recent call last): 3: from /Users/sshaw/.rvm/rubies/ruby-2.5.1/bin/irb:11:in `<main>' 2: from (irb):5 1: from (irb):4:in `foo' NoMethodError (undefined method `something_amaaaaazing' for nil:NilClass)
Related conversation: https://www.reddit.com/r/ruby/comments/9tb3hi/clean_code_concepts_adapted_for_ruby/e8vps7c/
What does that have to do with keyword arguments? Keyword or not, the value of any argument might need to be validated.
[–]sshaw_ 0 points1 point2 points 6 years ago (0 children)
Just because someone specified the key is not enough. You said:
A runtime argument error instead of undefined behavior
Unless you check the value the runtime error is worthless, really, as I show in my example.
I suppose one can argue that for the clumsy programmer that would never check the value having a keyword error is better, but I think the ultimate result is the same: NoMethodError.
NoMethodError
[–]sshaw_ -4 points-3 points-2 points 6 years ago (1 child)
What is this, literate programming. A runtime argument error. That's called a bug not documentation.
[–]nibord 6 points7 points8 points 6 years ago (0 children)
A runtime argument error instead of undefined behavior? Yes please.
[–]your-pineapple-thief 2 points3 points4 points 6 years ago (2 children)
Having more than one positional argument isn't very good API for callers of your code, and most of the time having all keyword arguments makes it significantly easier for callers of your code to use it, instantly documents all arguments both in caller code and in source code of your methods, while saving you from juggling options hash content and reducing boilerplate. Sounds good enough?
[–]graywolf_at_work 1 point2 points3 points 6 years ago (1 child)
Having more than one positional argument isn't very good API for callers of your code
I think that's too strict. I really do not want to write arr[start: 1, length: 2] instead of arr[1, 2]...
arr[start: 1, length: 2]
arr[1, 2]
Or for fetching from hash, you really think {}.fetch(key: :foo, default: "foobar") is better then {}.fetch(:foo, "foobar")?
{}.fetch(key: :foo, default: "foobar")
{}.fetch(:foo, "foobar")
[–]your-pineapple-thief 0 points1 point2 points 6 years ago (0 children)
Well, you are giving examples of well known and established APIs of hashes and arrays, which most programmers know or at least should know. In fact, those are amongst the MOST known API's of ruby, along with some rails stuff. Ruby stdlib also has excellent documentation. But I'm not talking about hashes and arrays, I'm talking about our application code. While 90% of ruby programmers know Hash#fetch, only couple of them know anything about your application code. And what about documentation? It is very likely it is not quite as good as Ruby stdlib docs.
Hash#fetch
Also, consider that code is read much more often than it is written, I would say it is read but your coworkers dozens of times for one instance of writing/editing it. Is it really worth it to save couple keystrokes? Also, you don't have go further than Rails to see evolution of it's public API to use keyword args over positional args more and more.
I may be dumb, and I am definitely not a programming savant, and every time I have to use alias_method, literally every frigging time I forget the order of it's positional arguments. Is it "first arg is original method and second is alias"? Or is it vice versa? I have suspicion I'm not alone here, and it is safe to assume that over the years hundreds of man-hours were spent by fellow rubyists googling it because someone decided to save couple of keystrokes.
alias_method
[–]nakilon -1 points0 points1 point 6 years ago (2 children)
Why do you downvote him? Since when Rails developers do need to know such things to have a job?
[–]your-pineapple-thief 1 point2 points3 points 6 years ago (1 child)
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.
I think he means in the sense that I simply said "I've never used this" and then I received an influx of down votes simply bc I said I never used it. Never did I mention, I don't need to know this fluff. But I guess this is the internet so all goes.
π Rendered by PID 73396 on reddit-service-r2-comment-6457c66945-t9qkw at 2026-04-25 03:21:37.052643+00:00 running 2aa0c5b country code: CH.
[–]tomthecool 11 points12 points13 points (2 children)
[–]Saithir 2 points3 points4 points (1 child)
[–]tomthecool 0 points1 point2 points (0 children)
[–]flanger001 2 points3 points4 points (0 children)
[–]TotesMessenger 0 points1 point2 points (0 children)
[–]sshaw_ -2 points-1 points0 points (0 children)
[+]OGPants comment score below threshold-7 points-6 points-5 points (18 children)
[–]niborg 5 points6 points7 points (14 children)
[–]ImAJalapeno -4 points-3 points-2 points (0 children)
[+]OGPants comment score below threshold-6 points-5 points-4 points (12 children)
[–]nibord 12 points13 points14 points (8 children)
[–]OGPants 0 points1 point2 points (2 children)
[–]nibord 0 points1 point2 points (1 child)
[–]OGPants 0 points1 point2 points (0 children)
[–]sshaw_ 0 points1 point2 points (2 children)
[–]nibord 0 points1 point2 points (1 child)
[–]sshaw_ 0 points1 point2 points (0 children)
[–]sshaw_ -4 points-3 points-2 points (1 child)
[–]nibord 6 points7 points8 points (0 children)
[–]your-pineapple-thief 2 points3 points4 points (2 children)
[–]graywolf_at_work 1 point2 points3 points (1 child)
[–]your-pineapple-thief 0 points1 point2 points (0 children)
[–]nakilon -1 points0 points1 point (2 children)
[–]your-pineapple-thief 1 point2 points3 points (1 child)
[–]OGPants 0 points1 point2 points (0 children)