2011-03-29 40 views
0

我嘗試重構我這個代碼:andand鏈不能正常工作

test_cases.select{ |x| x.script && x.script.versions && !x.script.versions.empty? } 

,並使用andand寶石。基本用法只是告訴我們,替代andand到& &(因而得名),所以我想這:

test_cases.select{ |x| !x.andand.script.andand.script_versions.andand.empty? } 

,但現在看來,這是不是-ING整個語句(我才意識到這個現在一邊寫問題)

這是否意味着我必須分開空?條款?是否有檢查不是andand功能

回答

0

我實際使用這一條款,它的工作原理:

test_cases.map{ |x| x.script.andand.versions && !x.script.versions.empty? }.none? 

只有一個andand調用和使用的選擇

0

你總是可以做

test_cases.select{ |x| x.andand.script.andand.script_version } unless x.empty? 

andand話題(但只有最後一個?):雖然我們在我們的項目中使用它,我通常很喜歡它,我發現虛假使用andand可以隱藏應該妥善解決的問題。像許多好工具一樣,它必須謹慎使用。

0

地圖,而不是當xnil它工作得很好,我猜。問題是當xnil,因爲...empty?還返回nil!niltrue。然後這個x被錯誤地選中。我提出了布爾值的明確檢查:

test_cases.select { |x| x.andand.script.andand.versions.andand.empty? == false } 

或者可能是更好的使用空/目前沒有最後andand

test_cases.select { |x| x.andand.script.andand.versions.present? } 
test_cases.reject { |x| x.andand.script.andand.versions.blank? } 

注:這不是與問題相關的在這裏,但我更喜歡Ick的也許超過,因爲只有nil被視爲「無效」(false是我認爲不應被代理的合法值)。