Given a map of items to their attributes:
OPTIONS = {
paper: %w[A4_paper countable universal],
pencil: %w[trackable universal trackable],
chalk: %w[stationery countable trackable A4_paper],
}.freeze
I need to return the items that have a particular attribute. For example, fetch_type_values('countable') should return ["paper", "chalk"].
Here's my working implementation:
def fetch_type_values(option)
OPTIONS.map { |key, value| key if value.include?(option.to_s) }.compact.map(&:to_s)
end
I feel that this is a bit difficult to read; How can I refactor/simplify it to make it more readable?
.to_s()more times than necessary -- but that's a concise readability versus CPU cycles tradeoff. For "small"optionarguments, the code as written is better than assigning a temp var. \$\endgroup\$