2012-04-26 13 views

답변

4
unless current_user.received_replies.unread.empty? 
    # ... 
end 

아니면 ifelse가있는 경우 (unless/else 지옥으로 못 생기고 있기 때문에), 사례를 전환 :

if current_user.received_replies.unread.empty? 
    # ... 
else 
    # ... 
end 
1

이 조금 더 좋을 수 :

unless current_user.received_replies.unread.empty? 
2

나는 다음과 같이 쓸 것이다 :

if current_user.received_replies.unread.any? 

docs :

 
= Array.any? 

(from ruby core) 
=== Implementation from Enumerable 
------------------------------------------------------------------------------ 
    enum.any? [{|obj| block } ] -> true or false 

------------------------------------------------------------------------------ 

Passes each element of the collection to the given block. The method returns 
true if the block ever returns a value other than false or nil. If the block 
is not given, Ruby adds an implicit block of {|obj| obj} (that is any? will 
return true if at least one of the collection members is not false or nil. 

     %w{ant bear cat}.any? {|word| word.length >= 3} #=> true 
     %w{ant bear cat}.any? {|word| word.length >= 4} #=> true 
     [ nil, true, 99 ].any?       #=> true 
+1

위자료 항목이 목록에있는 경우에는 그와 완전히 동일하지 않습니다. 'ActiveRecord'의 영역에서는 실용적 일지 모르지만, 사람들이 '공백'에 대한 부정이라고 생각한 부분에 대해 많은 혼란을 겪었습니다. 이것은 일반적으로 사실이 아닙니다. –

+0

@ NiklasB. 와우 나는 그것을 결코 알아 채지 못했습니다. '[false] .any?'가'false' 인 것은 더러운 것입니다. –

+0

@ Andrew : 왜 그럴까? 보통이 함수는 '[1,2,3] .any?'와 같이 항목의 * any *에 대한 술어가 있는지 확인합니다. {| x | x> 2}'이다. 블록 없이도 작동한다는 것은 유감 스럽습니다 (어디에서 유용 할 지 모르겠습니다). –

관련 문제