我有一个方法如下:如何摆脱case语句的红宝石
def something(param)
case param
when 1
break if <already executed for some similar data>
#Some code
when 2
#Some code
else
#Some code
end
end
如果param
是1
,我将创建数据库的记录。在这种情况下,我需要检查相同的数据是否已经存在,如果有的话,我需要打出when
。
当我尝试break if <condition>
,我得到这个错误:
Can't escape from eval with break
作为一个解决方案,case语句之前,我可以改变param
的值,以便它(when 1
)从未满足条件:
def something(param)
param = 0 if <condition>
case param
when 1
.
.
但我觉得它很丑。
break
是用来摆脱循环,但我需要在这里它的一个等价物,像switch语句break。
a = 1
b = 1
case a
when 1
return if b == 1
p "Code Executed"
end
给我:
LocalJumpError: unexpected return
可能重复:http://stackoverflow.com/questions/8022865/how-to-break-out-of-a-case-while- in-ruby –
@LucasNelson嘿,卢卡斯。感谢您指点这篇文章。但是,请你看看我的编辑。 – Abhi
从答案中的评论,感觉就像你现在排序。 –