!(双重爆炸)意思是Ruby

Lir*_*roy 4 ruby

!!以下方法意味着什么?

def include?(record)
  !!@association.include?(record)
end
Run Code Online (Sandbox Code Playgroud)

hsp*_*her 6

它将变量转换为类型boolean并确定其truthy或falsy值

例如:-

# Numbers...
!!1 # => true
!!0 # => true

# Numbers as strings...
!!'1' # => true
!!'0' # => false

# Truthy strings (case insensitive)...
!!'true'  # => true  (alias: 't')
!!'false' # => false (alias: 'f')
!!'yes'   # => false (alias: 'y')
!!'no'    # => false (alias: 'n')

# Booleans...
!!true  # => true
!!false # => false

# Nil...
!!nil # => false
Run Code Online (Sandbox Code Playgroud)

  • !! 0是真的.[只有`nil`和`false`在Ruby中是假的](/sf/ask/2270333481/) (2认同)