代码重构以删除重复的正则表达式

时间:2015-09-17 03:50:58

标签: ruby refactoring

我写了一段Ruby代码,用于从html页面中提取信息。

  combined = state = county = special = 0

  unless options.nil?

    unless /([0-9\.]+)% \(Combined\)/.match(options).nil?
      combined = /([0-9\.]+)% \(Combined\)/.match(options)[1].to_f
    end

    unless /([0-9\.]+)% \(State\)/.match(options).nil?
      state = /([0-9\.]+)% \(State\)/.match(options)[1].to_f
    end

    unless /([0-9\.]+)% \(County\)/.match(options).nil?
      county = /([0-9\.]+)% \(County\)/.match(options)[1].to_f
    end

    unless /([0-9\.]+)% \(Special\)/.match(options).nil?
      special = /([0-9\.]+)% \(Special\)/.match(options)[1].to_f
    end

    if combined==0 and state==0 and county==0 and special ==0 then
      unless />([0-9\.]+)%</.match(options).nil?
        combined = />([0-9\.]+)%</.match(options)[1].to_f
      end          
    end

  end

我应该如何重构此代码以删除每个正则表达式的重复?

2 个答案:

答案 0 :(得分:2)

更新:相同的方法,但稍微清理了代码

results = Hash.new(0)
if options
  %w(Combined State County Special).each do |query|
    options =~ /([0-9\.]+)% \(#{query}\)/
    results[query.downcase.intern] = $1.to_f if $~
  end

  if results.values.all?(&:zero?)
    options =~ />([0-9\.]+)%</
    results[:combined] = $1.to_f if $~
  end
end

答案 1 :(得分:2)

return if options.nil?
options.scan(/([0-9.]+)% \(([\w]+)\)/) do
  case $2
  when "Combined".freeze then combined = $1.to_f
  when "State".freeze    then state    = $1.to_f
  when "County".freeze   then county   = $1.to_f
  when "Special".freeze  then special  = $1.to_f
  else
    combined = $1.to_f if options =~ />([0-9.]+)%</
  end
end