开发者

Any way to refactor this code smaller?

I did a pretty good hack and slash on the original code, but I'm not seeing any way to condense this smaller without farming the checks into another file in a module. The regex text is different for each when/x/, so they can't be combined any further that I can think of

    case state
      when /OH|PA|MN/
        if @browser.text.include?("My text")
            raise "x" unless /foo/.match(@browser.text)
            raise "y" unless /foo2/.match(@browser.text)
            raise "z" unless /foo3/.match(@browser.text)
        else
            raise "x1" unless /foofoo/.match(@browser.text)
            raise "y1" unless /foofoo2/.match(@browser.text)
            raise "z1" unless /foofoo3/.match(@browser.text)
        end
      when /IL|VA/
        if @browser.text.include?(开发者_如何转开发"My text")
            raise "x" unless /foo/.match(@browser.text)
            raise "y" unless /foo2/.match(@browser.text)
            raise "z" unless /foo3/.match(@browser.text)
        else
            raise "x1" unless /foofoo/.match(@browser.text)
            raise "y1" unless /foofoo2/.match(@browser.text)
            raise "z1" unless /foofoo3/.match(@browser.text)
        end
      when /WI|SC|TN|IN|IA/
        if @browser.text.include?("My text")
            raise "x" unless /foo/.match(@browser.text)
            raise "y" unless /foo2/.match(@browser.text)
            raise "z" unless /foo3/.match(@browser.text)
        else
            raise "x1" unless /foofoo/.match(@browser.text)
            raise "y1" unless /foofoo2/.match(@browser.text)
            raise "z1" unless /foofoo3/.match(@browser.text)
        end
      when /SC/
        if @browser.text.include?("My text")
            raise "x" unless /foo/.match(@browser.text)
            raise "y" unless /foo2/.match(@browser.text)
            raise "z" unless /foo3/.match(@browser.text)
        else
            raise "x1" unless /foofoo/.match(@browser.text)
            raise "y1" unless /foofoo2/.match(@browser.text)
            raise "z1" unless /foofoo3/.match(@browser.text)
        end
      when /GA/
        if @browser.text.include?("My text")
            raise "x" unless /foo/.match(@browser.text)
            raise "y" unless /foo2/.match(@browser.text)
            raise "z" unless /foo3/.match(@browser.text)
        else
            raise "x1" unless /foofoo/.match(@browser.text)
            raise "y1" unless /foofoo2/.match(@browser.text)
            raise "z1" unless /foofoo3/.match(@browser.text)
        end
      else
        raise "Not a valid state"
      end


i assume that its differnt in your real code so that you couldnt do

if state =~/OH|PA|MN|IL|VA|WI|SC|TN|IN|IA|SC|GA/
    if @browser.text.include?("My text")
        raise "x" unless /foo/.match(@browser.text)
        raise "y" unless /foo2/.match(@browser.text)
        raise "z" unless /foo3/.match(@browser.text)
    else
        raise "x1" unless /foofoo/.match(@browser.text)
        raise "y1" unless /foofoo2/.match(@browser.text)
        raise "z1" unless /foofoo3/.match(@browser.text)
    end
else
    raise "Not a valid state"
end

because right now you can. So if you really wanted an answer why did you post fake code that can be easily reduced?


raise "Not a valid state" unless %w[OH PA MN IL VA WI SC TN IN IA SC GA].include?(state)
if @browser.text.include?("My text")
  raise case @browser.text
  when /foo/; "x"
  when /foo2/; "y"
  when /foo3/; "z"
  end
else
  raise case @browser.text
  when /foofoo/; "x1"
  when /foofoo2/; "y1"
  when /foofoo3/; "z1"
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜