Created
January 14, 2016 08:12
-
-
Save YumaInaura/75da9d1b3cd8912c451b to your computer and use it in GitHub Desktop.
Ruby | 正規表現でのキャプチャがマッチしなかった場合の NoMethodError に対策する ref: http://qiita.com/Yinaura/items/d40ea26f0f69afc92a81
This file contains hidden or 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
| 'Welcome to my DIRTY house.'.match(/(?<match>DIRTY)/)[:match] | |
| # => "DIRTY" |
This file contains hidden or 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
| 'Welcome to my DIRTY house.'.match(/(?<match>CLEAN)/)[:match] | |
| # NoMethodError: undefined method `[]' for nil:NilClass |
This file contains hidden or 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
| nil.to_h | |
| # => {} |
This file contains hidden or 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
| 'Welcome to my DIRTY house.'.match(/(?<match>CLEAN)/).to_h[:match] | |
| # => nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment