1
0

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.

感嘆符の備忘録

Posted at

感嘆符について自分自身の備忘録として残しておきます。

論理演算子(!)

条件式の前に「!」を付ける
条件式を否定する
ex)
aがfalseの場合true
!a #-> true

二重感嘆符(!!)

返り値をtrueかfalseにする
ex)
!nil #=> true
!!nil #=> false
!!(false && nil) #=> false
!!(nil && nil) #=> false

例外を発生させる(create!)

  • createやsaveに「!」を付ける
    メソッド名に!がついてるものは失敗時に例外を発生させる
    メソッド名に!がついてるものは失敗時にnilを返す
    rescueで拾える
    service内ではrescueしてエラーセットしてコントローラでエラーを元にrenderしてる

find_byに「!」を付ける(find_by!)

find_by!だとnotfoundのexceptionが発生する
find_byで見つからない場合は返り値nil

比較演算子(hoge != foo)

a != b #aとbが等しくない

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?