這是什麼功能做什麼?
def current_product?
!!current_product
end
這不是雙重否定嗎?
這是什麼功能做什麼?
def current_product?
!!current_product
end
這不是雙重否定嗎?
!!
基本上是布爾鑄造。如果current_product
是truthy,!current_product
是false
和!!current_product
是true
,反之亦然。即它將truthy的值轉換爲true
和falsy的值爲false
。
這實際上是一個強制轉換/布爾轉換。
類似的問題,但對於C++:Doube Negation in C++ code
也是一個相當不錯的職位約在這裏:!! (The double bang/double not) in Ruby
這是你會在每個對象都有一個真值的任何語言看到一個模式,但也有規範布爾值(無論它們被稱爲True
和False
,1
和0
,1
和""
,t
和nil
, 隨你)。 !!x
本質上是「強制轉換爲布爾值」,因爲!!x
與x
具有相同的真值,但!!x
將始終是規範的真/假值之一,而不是任何舊的真/假值。
重複[什麼!意思是在紅寶石?](http://stackoverflow.com/questions/524658/what-does-mean-in-ruby)。 – 2010-12-09 02:07:04