Last active
September 18, 2018 01:11
-
-
Save mexelout/960025822afe45afea68f5ee56dfe3a8 to your computer and use it in GitHub Desktop.
ActiveSupportのtryメソッド ref: https://qiita.com/mexelout/items/567a55108155230d9337
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hoge.first.try(:name) | |
# => メソッドや変数の呼び出しで、objがnilでも例外を発生させずに通れる |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hoge.first.try { |hoge| { id: hoge.id, name: hoge.name } } | |
# => {:id => 1, :name => 'bar'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hoge.first.yield_self { |hoge| { id: hoge.id, name: hoge.name } } | |
# => { :id => 1, :name => 'bar' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hoge.first.then { |hoge| { id: hoge.id, name: hoge.name } } | |
# => { :id => 1, :name => 'bar' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment