LoginSignup
0
0

More than 5 years have passed since last update.

booleanに対するrailsのpresent?

Posted at

コードレビューしててダメじゃんと思ったけどダメじゃなかった件のメモ

端的に書くとこんなコード(例なので強引にpresent?使ってます)

hoge = fuga.piyo? # true or falseを返す

if hoge.present?
  p "hogehoge"
end

trueにしろfalseにしろif文の中通っちゃうと思ったけどそうじゃなかった。

present?の確認

> false.present?
=> false

> [nil, true, false, 0, 1, ""].map(&:present?)
=> [false, true, false, true, true, false]

変数にfalseが入っていてもpresent?ではfalseを返すことが確認できました。
この時点では違和感が。

railsのコードを見ると
https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/blank.rb#L23

blank?の裏返し。

> false.blank?
=> true

> [nil, true, false, 0, 1, ""].map(&:blank?)
=> [true, false, true, false, false, true]

falseはtrueを返すことを確認。
こっちだと違和感ないから不思議。

納得したので終了

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0