Skip to main content
Post Reopened by Toby Speight, Sᴀᴍ Onᴇᴌᴀ
Update wording
Source Link

Given a map fromof 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; any suggestions how IHow can I refactor/simplify it to make it more readable?

Given a map from 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; any suggestions how I can refactor/simplify it to make it more readable?

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?

Describe the intent as I understand it
Added to review
Source Link
Toby Speight
  • 88.3k
  • 14
  • 104
  • 327

Fetch options which include a given value Find items by their properties

I haveGiven a methodmap from items to fetch keys based on a value passed in as an argument

def fetch_type_values(option)
 OPTIONS.map { |key, value| key if value.include?(option.to_s) }.compact.map(&:to_s)
end

OPTIONS are astheir attributes:

    OPTIONS = {
      paper: %w[A4_paper countable universal],
      pencil: %w[trackable universal trackable],
      chalk: %w[stationery countable trackable A4_paper],
    }.freeze

when we pass an optionI need to return the methoditems that have a particular attribute. For example, say fetch_type_values('countable'), it should return    ["paper", "chalk"], which it does, and it's working how it's supposed to.

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 just feel the methodthat this is a bit difficult to read,read; any waysuggestions how I can refactor/simplify it to make it more readable?

Fetch options which include a given value

I have a method to fetch keys based on a value passed in as an argument

def fetch_type_values(option)
 OPTIONS.map { |key, value| key if value.include?(option.to_s) }.compact.map(&:to_s)
end

OPTIONS are as:

    OPTIONS = {
      paper: %w[A4_paper countable universal],
      pencil: %w[trackable universal trackable],
      chalk: %w[stationery countable trackable A4_paper],
    }.freeze

when we pass an option to the method, say 'countable', it should return  ["paper", "chalk"], which it does, and it's working how it's supposed to.

I just feel the method is a bit difficult to read, any way I can refactor/simplify it to make it more readable?

Find items by their properties

Given a map from 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; any suggestions how I can refactor/simplify it to make it more readable?

Post Closed as "Not suitable for this site" by ggorlen, pacmaninbw, AJNeufeld
Summarise the purpose, not the concerns, in the title
Source Link
Toby Speight
  • 88.3k
  • 14
  • 104
  • 327

Ruby: simplify the method to fetch keys based on an input Fetch options which include a given value

I have a method to fetch keys based on a value passed in as an argument

def fetch_type_values(option)
 OPTIONS.map { |key, value| key if value.include?(option.to_s) }.compact.map(&:to_s)
end

OPTIONS are as:

    OPTIONS = {
      paper: %w[A4_paper countable universal],
      pencil: %w[trackable universal trackable],
      chalk: %w[stationery countable trackable A4_paper],
    }.freeze

when we pass an option to the method i.e. 'countable', say 'countable', it should return ["paper", "chalk"]["paper", "chalk"], which it does, and it's working how it's supposed to.

I just feel the method is a bit difficult to read, any way I can refactor/simplify it to make it more readable?

Ruby: simplify the method to fetch keys based on an input value

I have a method to fetch keys based on a value passed in as an argument

def fetch_type_values(option)
 OPTIONS.map { |key, value| key if value.include?(option.to_s) }.compact.map(&:to_s)
end

OPTIONS are as:

    OPTIONS = {
      paper: %w[A4_paper countable universal],
      pencil: %w[trackable universal trackable],
      chalk: %w[stationery countable trackable A4_paper],
    }.freeze

when we pass an option to the method i.e. 'countable' it should return ["paper", "chalk"], which it does, and it's working how it's supposed to.

I just feel the method is a bit difficult to read, any way I can refactor/simplify it to make it more readable?

Fetch options which include a given value

I have a method to fetch keys based on a value passed in as an argument

def fetch_type_values(option)
 OPTIONS.map { |key, value| key if value.include?(option.to_s) }.compact.map(&:to_s)
end

OPTIONS are as:

    OPTIONS = {
      paper: %w[A4_paper countable universal],
      pencil: %w[trackable universal trackable],
      chalk: %w[stationery countable trackable A4_paper],
    }.freeze

when we pass an option to the method, say 'countable', it should return ["paper", "chalk"], which it does, and it's working how it's supposed to.

I just feel the method is a bit difficult to read, any way I can refactor/simplify it to make it more readable?

Source Link
Loading