1
votes

I wrote following piece of Ruby code which is used to extract information from an html page.

  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

How should I refactor this code to remove repetition of each regular expression?

2

2 Answers

2
votes
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
2
votes

Update: Same approach but cleaned up code a bit

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