Skip to content

Instantly share code, notes, and snippets.

@jah2488
Created January 31, 2014 15:33
Show Gist options
  • Save jah2488/8734298 to your computer and use it in GitHub Desktop.
Save jah2488/8734298 to your computer and use it in GitHub Desktop.
Playing around with ruby's new keyword arguments to improve method clarity
def extract(from_record:, attribute:)
from_record[attribute]
end
def parse(path)
get_data_from(path).map do |record|
output_class.new(*attributes.map { |name| extract(attribute: name, from_record: record) })
end
end
def extract(record, attr)
record[attr]
end
def convert(path)
get_data_from(path).map do |record|
output_class.new(*attributes.map { |attribute| extract(record, attribute) })
end
end
@jah2488
Copy link
Author

jah2488 commented Jan 31, 2014

I was working on a programming puzzle yesterday and decided to try out ruby's new keyword arguments to see if I could improve the readability of my methods. It always bothers me when a method is so close to reading like a sentence and instantly being clear, but instead is garbled up by positional arguments. I'm not the biggest fan of the verbosity of ObjC in general, but I do really enjoy the readability of their methods with their interspersed arguments. While this is clearly not an equivalent of those, it is a way of achieving some of the same benefit.

I'm not sure if this has sold me on using keyword arguments going forward, but I would like to see the best places that I can apply it. The added benefit of not being order dependent could also stop silly quibbles over which order would be more readable for a given person.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment