Keyword Arguments of Ruby makes code more clear
chenge
Dec 26 '18
・1 min read
In this video the speaker demoed the pros of KWArgs.
In the pic, upper code is obviously more clear.
So when should you use it? I think, when it's hard to identify the params you should use KWArgs.
The usage is simple:
def foo(bar:, baz:)
end
foo(bar: 1, baz: 2)
Do you think so?
Classic DEV Post from Jan 2

Easily Build GraphQL APIs with Prisma
Todd Birchard - Feb 4

Protect your system from changes in 3rd party dependencies
Aleksi Kauppila - Feb 4

Monolithic vs. Microservices Architecture: Which is Right for Your App
Aleksandra - Jan 31

Didn't watch the video, but what I find interesting is that keyword arguments did not exist until Ruby 2.0, which means before Ruby 2.0, there's a totally different way to do hack it, and you'll sometimes see this in the wild :) (I've encountered this in Rails' DateTime library)
robots.thoughtbot.com/ruby-2-keywo...
Yes, it simplify code. Very useful.
Seems right as shown, but perhaps only for
.new
?For any other method:
If the parameters are related, could they form a class and be passed as an object?
If not, does the method lack a unified purpose?
Right, better params no more than 3. If more than 3 maybe should use hash or object.
How does it handle extra arguments? Does it ignore it or throw an exception?
ArgumentError