重构条件变量赋值

Jos*_*sto 5 ruby refactoring

我正在做一个项目.目前我有一个相当大的条件语句,它根据一些输入参数为变量赋值.所以,我有这样的事情.

if some condition
  x = some value
elsif another condition
  x = a different value
  ...
Run Code Online (Sandbox Code Playgroud)

重构这个的最佳方法是什么?我希望我最终会得到像这样的东西

x = some value if some condition || another value if another condition
Run Code Online (Sandbox Code Playgroud)

有这种事情的模式吗?

Chu*_*uck 12

只需将作业放在if之外.

x = if some condition
  some value
elsif another condition
  a different value
Run Code Online (Sandbox Code Playgroud)

或者你可以使用哈希.

x = dict[some condition]
Run Code Online (Sandbox Code Playgroud)