Skip to main content
4 of 4
Update wording

Find items by their properties

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?