Fun with keyword arguments, hashes, and splats

You’ve probably seen this pattern before. A method has an options hash as its last argument, which holds extra parameters:

1 2 3 4 5 6 def hello_message(name_parts = {}) first_name = name_parts.fetch(:first_name) last_name = name_parts.fetch(:last_name) "Hello, #{first_name} #{last_name}" end

Unfortunately, you need to extract those parameters out of the hash. And that means there’s a lot of setup to wade through before you get to the good part.

But if you changed this method to use keyw...

 •  0 comments  •  flag
Share on Twitter
Published on April 04, 2015 14:39
No comments have been added yet.