我正在做一个项目。目前我有一个相当大的条件语句,它根据一些输入参数为变量赋值。所以,我有这样的东西。
if some condition
x = some value
elsif another condition
x = a different value
...
重构它的最佳方法是什么?我希望我最终会得到类似的东西
x = some value if some condition || another value if another condition
这种事情有规律吗?
最佳答案
只需将赋值放在 if 之外即可。
x = if some condition
some value
elsif another condition
a different value
或者您可以使用哈希。
x = dict[some condition]
关于ruby - 重构条件变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15535831/