编写基于 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()允许您使用字符串或符号按名称调用方法。第一个参数是方法的名称;以下参数只是传递给被调用的方法。

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

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

相关内容

最新更新