Created
January 31, 2014 15:33
-
-
Save jah2488/8734298 to your computer and use it in GitHub Desktop.
Playing around with ruby's new keyword arguments to improve method clarity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.