16
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

present?メソッドとpresenceメソッドの違い

Last updated at Posted at 2018-09-30

present?メソッドとは

対象のオブジェクトがnil, "", " "(半角スペース), , {}(空のハッシュ)の時にfalase,それ以外の場合はtrueを返すメソッド(!blank?と同じ)。

engineer = 'aoki'
engineer.present?
=> true

engineer = nil
engineer.present?
=> false

engineer = ""
engineer.present?
=> false

presenceメソッドとは

present?の結果がtrueの時、レシーバ自身を返し、`present? の結果が false のときは nil を返すメソッド。

engineer = 'aoki'
engineer.presence
=> 'aoki'

engineer = nil
engineer.presence
=> nil

engineer = ""
engineer.presence
=> nil

参考

[ActiveSupport の便利メソッド: try, blank?, present?, presence]
(http://www.techscore.com/blog/2012/12/25/activesupport-%E3%81%AE%E4%BE%BF%E5%88%A9%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89-try-blank-present-presence/)

16
7
3

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
16
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?