2014-10-20 16 views
1

我正在寫一些像這樣的代碼在Ruby中:Ruby中用於(a == 0 || a == foo)的簡短方式?

if very_long_variable_name_that_cant_be_shortened == 0 || very_long_variable_name_that_cant_be_shortened == foo 

這顯然長,忽略了最低看起來非常好。有什麼辦法可以寫這個更短嗎?

在此先感謝。

+0

你檢查''包括? – shiva 2014-10-20 08:43:25

+0

我們是否假設'very_long_variable_name'是一個'Hash'? – PericlesTheo 2014-10-20 08:44:43

+0

如何創建一個局部變量? – 2014-10-20 08:46:25

回答

7

您可能需要使用include?

>> [0, foo].include? very_long_variable_name_that_cant_be_shortened 
=> true 
4
case very_long_variable_name_that_cant_be_shortened 
when 0, foo 
    ... 
end 
+1

其實,我沒有拿出'case'。謝謝。 – osyoyu 2014-10-20 08:51:17

-1
def helperMethd (f): 
    return foo == 0 || f == foo; 

if helperMethd(very_long_variable_name_that_cant_be_shortened): 
    bla bla 
相關問題