我怎么能干这个代码而不是放很多条件

时间:2013-02-20 14:00:56

标签: ruby

我正在使用ROR并且在我的控制器功能中我重新获得了这些参数的参数和基数我需要根据条件执行动作。但我发现这些约有18个条件。

我怎样才能干掉这段代码。

if params[:topic] == "Topic (title)" and params[:sort] == "Date (ASC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Date (DESC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Topic (ASC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Topic (DESC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Author (ASC)"
  # custom code
elsif params[:topic] == "Topic (title)" and params[:sort] == "Author (DESC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Date (ASC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Date (DESC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Topic (ASC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Topic (DESC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Author (ASC)"
  # custom code
elsif params[:topic] == "Post (body)" and params[:sort] == "Author (DESC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Date (ASC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Date (DESC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Topic (ASC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Topic (DESC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Author (ASC)"
  # custom code
elsif params[:topic] == "Author" and params[:sort] == "Author (DESC)"
  # custom code
end

非常感谢

3 个答案:

答案 0 :(得分:1)

使用case... when语法。会让事情变得更加清晰。同时将所有ifs的第一部分移出案例 - 它们似乎在很多情况下都很常见。

答案 1 :(得分:1)

如果代码合法地不同,请考虑一个案例陈述:

case [params[:topic], params[:sort]]
when ["Topic (title)", "Date (ASC)"]
  # custom code
when ["Topic (title)", "Date (DESC)"]
  # custom code
when ["Topic (title)", "Topic (ASC)"]
  # custom code
when ["Topic (title)", "Topic (DESC)"]
  # custom code
when ["Topic (title)", "Author (ASC)"]
  # custom code
when ["Topic (title)", "Author (DESC)"]
  # custom code
when ["Post (body)", "Date (ASC)"]
  # custom code
when ["Post (body)", "Date (DESC)"]
  # custom code
when ["Post (body)", "Topic (ASC)"]
  # custom code
when ["Post (body)", "Topic (DESC)"]
  # custom code
when ["Post (body)", "Author (ASC)"]
  # custom code
when ["Post (body)", "Author (DESC)"]
  # custom code
when ["Author", "Date (ASC)"]
  # custom code
when ["Author", "Date (DESC)"]
  # custom code
when ["Author", "Topic (ASC)"]
  # custom code
when ["Author", "Topic (DESC)"]
  # custom code
when ["Author", "Author (ASC)"]
  # custom code
when ["Author", "Author (DESC)"]
  # custom code
end

如果代码中有重复,或者您最终在多个位置使用此case语句,则可能有更好的方法来执行此操作。

答案 2 :(得分:0)



  topics = ['Topic (title)', 'Post (body)', 'Author' ]
  sorts = ['Date (ASC)', 'Date (DESC)', 'Topic (ASC)', 'Topic (DESC)']
  code_map = {
    [topics[0], sorts[0]] => ->() {
      # custom code
    },
    [topics[0], sorts[1]] => ->() {
      # custom code
    }
  }

  # usage
  code_map[[params[:topic], params[:sort]]()

虽然这样可以消除重复,但我不相信你想沿着这条路走下去,因为我相信'自定义代码'也会有很多重复。

向我们展示您的“自定义代码”以及它从一个案例到下一个案例的不同之处,我们可以为您干掉而不是在调度级别