我在做一个项目。目前我有一个相当大的条件语句,它根据一些输入参数为变量赋值。所以,我有这样的东西。
if some condition
x = some value
elsif another condition
x = a different value
...重构的最好方法是什么?我希望我能以这样的方式结束
x = some value if some condition || another value if another condition这类事情有没有模式?
发布于 2013-03-21 06:27:38
只要将赋值放在if之外即可。
x = if some condition
some value
elsif another condition
a different value或者您可以使用Hash。
x = dict[some condition]发布于 2013-03-21 06:23:33
它不是一个模式,而是一个运算符。您所指的是三元运算符:
If Condition is true ? Then value X : Otherwise value Y下面是一个示例:
speed = 90
speed > 55 ? puts("I can't drive 55!") : puts("I'm a careful driver")使用三元语句很简短,而且效果很好。
发布于 2013-03-21 06:27:41
x = some condition ? some value :
another condition ? a different value : ...https://stackoverflow.com/questions/15535831
复制相似问题