Railsでの0、空文字、nil、falseの扱いを調べてみた

## 概要
Rails で、`0`、`''`、`nil`、`false`の扱われかたを調べてみたメモ

## present?
`!blank?` と同義。

下記の条件の時に`false`を返す。[参考記事](http://railsdoc.com/references/present)

- `nil`
- `''`
- `' '`(半角スペースのみ)
- `[]`(空の配列)
- `{}`(空のハッシュ)

```rb
###########
# true
###########
val = 1
puts val.present? # => true

val = 0
puts val.present? # => true

val = true
puts val.present? # => true

###########
# false
###########
val = false
puts val.present? # => false

val = ''
puts val.present? # => false

val = ' ' # 半角スペース
puts val.present? # => false

val = nil
puts val.present? # => false

val = []
puts val.present? # => false

val = {}
puts val.present? # => false
```

## zero?
zeroかどうか調べる。
string, booleanでは`.zero`は使えず、エラーになる。

```rb
###########
# true
###########
val = 0
puts val.to_i.zero? # => true

val = ''
puts val.to_i.zero? # => true

val = nil
puts val.to_i.zero? # => true

# 文字列をto_iすると0になる
val = 'sample'
puts val.to_i # => 0
puts val.to_i.zero? # => true

# 半角スペースもstringと同じ
val = ' '
puts val.to_i # => 0
puts val.to_i.zero? # => true

###########
# false
###########
val = 1
puts val.to_i.zero? # => false

###########
# error
###########
# string
val = 'sample'
puts val.zero?
# => NoMethodError: undefined method `zero?' for "sample":String

# 半角スペースもstringと同じ
val = ' '
puts val.zero?
# => NoMethodError: undefined method `zero?' for " ":String

# boolean
val = true
puts val.zero?
# => NoMethodError: undefined method `zero?' for false:FalseClass
puts val.to_i.zero?
# => NoMethodError: undefined method `to_i' for true:TrueClass

# boolean
val = false
puts val.zero?
# => NoMethodError: undefined method `zero?' for false:FalseClass
puts val.to_i.zero?
# => NoMethodError: undefined method `to_i' for false:FalseClass

val = []
puts val.zero?
# => NoMethodError: undefined method `zero?' for []:Array
puts val.to_i.zero?
# => NoMethodError: undefined method `to_i' for []:Array

val = {}
puts val.zero?
# => NoMethodError: undefined method `zero?' for {}:Hash
puts val.to_i.zero?
# => NoMethodError: undefined method `to_i' for {}:Hash
```

## boolean?
そんなメソッドはない。
下記の判定式で、booleanかどうか判定できる。

```rb
########################
# is true? or is false?
########################
val = true
puts val.is_a?(TrueClass) # => true
puts val.is_a?(FalseClass) # => false

val = false
puts val.is_a?(TrueClass) # => false
puts val.is_a?(FalseClass) # => true

########################
# is boolean? == true
########################
val = true
puts val.is_a?(FalseClass) || val.is_a?(TrueClass) # => true

val = false
puts val.is_a?(FalseClass) || val.is_a?(TrueClass) # => true

########################
# is boolean? == false
########################
val = 1
puts val.is_a?(FalseClass) || val.is_a?(TrueClass) # => false

val = 0
puts val.is_a?(FalseClass) || val.is_a?(TrueClass) # => false

val = ''
puts val.is_a?(FalseClass) || val.is_a?(TrueClass) # => false

val = ' ' # 半角スペース
puts val.is_a?(FalseClass) || val.is_a?(TrueClass) # => false

val = nil
puts val.is_a?(FalseClass) || val.is_a?(TrueClass) # => false

val = 'sample'
puts val.is_a?(FalseClass) || val.is_a?(TrueClass) # => false

val = []
puts val.is_a?(FalseClass) || val.is_a?(TrueClass) # => false

val = {}
puts val.is_a?(FalseClass) || val.is_a?(TrueClass) # => false
```

以上

## 参考
- [ruby: nil? empty? blank? どれも0をチェックしない](http://kinopyo.com/ja/blog/ruby-nil-empty-blank-none-check-0)
- [Ruby Bool値かどうか型チェックしたい。ついでにis_bool?メソッドを作ってみた。](http://chaika.hatenablog.com/entry/2016/10/19/153728)
- http://railsdoc.com/references/present