根据 Ruby 中的一组条件编写 return 方法的更好方法

Better way to write methods that return methods based on a set of conditions in Ruby

我 运行 遇到了圈复杂度对于这个 ruby 方法来说太高的问题:

def find_value(a, b, lookup_value)
  return find_x1(a, b) if lookup_value == 'x1'
  return find_x2(a, b) if lookup_value == 'x2'
  return find_x3(a, b) if lookup_value == 'x3'
  return find_x4(a, b) if lookup_value == 'x4'
  return find_x5(a, b) if lookup_value == 'x5'
  return find_x6(lookup_value) if lookup_value.include? 'test'
end

有没有什么方法可以不用 eval 来写这个?

试试这个:

def find_value(a, b, lookup_value)
  return find_x6(lookup_value) if lookup_value.include? 'test'
  send(:"find_#{lookup_value}", a, b)
end

send() 允许您使用字符串或符号按名称调用方法。第一个参数是方法的名称;以下参数只是传递给被调用的方法。

如果您需要一些灵活性,查找方法或 class 名称没有错:

LOOKUP_BY_A_B = {
  'x1' => :find_x1,
  'x2' => :find_x2,
  'x3' => :find_x3,
  'x4' => :find_x4,
  'x5' => :find_x5,
}.freeze

def find_value(a, b, lookup_value)
  method = LOOKUP_BY_A_B[lookup_value]
  return self.send(method, a, b) if method
  find_x6(lookup_value) if lookup_value.include? 'test'
end

您还可以查找 Procs,例如

MY_PROCS = {
  1 => proc { |a:, b:| "#{a}.#{b}" },
  2 => proc { |a:, b:| "#{a}..#{b}" },
  3 => proc { |a:, b:| "#{a}...#{b}" }
}.freeze

def thing(a, b, x)
  MY_PROCS[x].call(a: a, b: b)
end