{"id":1302,"date":"2012-12-25T22:59:01","date_gmt":"2012-12-25T13:59:01","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=1302"},"modified":"2018-11-14T16:33:58","modified_gmt":"2018-11-14T07:33:58","slug":"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","status":"publish","type":"post","link":"https:\/\/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\/","title":{"rendered":"ActiveSupport \u306e\u4fbf\u5229\u30e1\u30bd\u30c3\u30c9: try, blank?, present?, presence"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>\u4fbf\u5229\u306a\u30e1\u30bd\u30c3\u30c9\u3084\u30a4\u30c7\u30a3\u30aa\u30e0\u306e\u5b58\u5728\u3092\u77e5\u3063\u3066\u3057\u307e\u3063\u305f\u3068\u304d\u306e\u697d\u3057\u3055\u306f\u3001Ruby\/Rails\u3092\u4f7f\u3046\u7406\u7531\u306e\u4e00\u3064\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001\u4fbf\u5229\u30e1\u30bd\u30c3\u30c9\u306e\u5b9d\u5eab ActiveSupport \u304b\u3089\u3044\u304f\u3064\u304b\u3054\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>try<\/h2>\n<p>try \u306f\u30ec\u30b7\u30fc\u30d0\u304c nil \u3067\u306a\u3051\u308c\u3070\u6307\u5b9a\u3057\u305f\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># @user \u304c nil \u306e\u5834\u5408\u306b NoMethodError \u304c\u767a\u751f\u3057\u3066\u3057\u307e\u3046\u306e\u3067 NG.\r\n@user.name\r\n\r\n# \u3042\u307e\u308a\u7dba\u9e97\u3067\u306f\u306a\u3044.\r\n@user &amp;&amp; @user.name\r\n\r\n# try \u3092\u4f7f\u3046\u3068\u3053\u3046\u306a\u308a\u307e\u3059.\r\n@user.try(:name)<\/pre>\n<p>\u30e1\u30bd\u30c3\u30c9\u304c\u5f15\u6570\u3092\u53d6\u308b\u5834\u5408\u306f\u3001try \u306e\u7b2c\u4e8c\u5f15\u6570\u4ee5\u964d\u306b\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># \u30e6\u30fc\u30b6\u306e\u914d\u5217 @users \u304c\u3042\u308c\u3070, \u5148\u982d\u304b\u3089 5 \u3064\u3092\u53d6\u5f97\u3059\u308b.\r\n@users.try(:take, 5)<\/pre>\n<p>\u30e1\u30bd\u30c3\u30c9\u304c\u30d6\u30ed\u30c3\u30af\u3092\u53d6\u308b\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">(1..10).try(:select){|x| x.even?}\r\n\r\n# \u6b21\u306e\u3088\u3046\u306b\u66f8\u304f\u3053\u3068\u3082\u3067\u304d\u307e\u3059.\r\n(1..10).try(:select, &amp;:even?)<\/pre>\n<p>&nbsp;<\/p>\n<h2>blank?<\/h2>\n<p>blank? \u306f\u30ec\u30b7\u30fc\u30d0\u304c\u4ee5\u4e0b\u306e\u5834\u5408\u306b true \u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30ec\u30b7\u30fc\u30d0\u304c nil \u306e\u5834\u5408<\/li>\n<li>\u30ec\u30b7\u30fc\u30d0\u304c false \u306e\u5834\u5408<\/li>\n<li>\u30ec\u30b7\u30fc\u30d0\u304c\u30db\u30ef\u30a4\u30c8\u30b9\u30da\u30fc\u30b9\u306e\u307f\u3092\u542b\u3080\u6587\u5b57\u5217\u306e\u5834\u5408<\/li>\n<li>\u30ec\u30b7\u30fc\u30d0\u304c\u7a7a\u306e\u914d\u5217\u306e\u5834\u5408<\/li>\n<li>\u30ec\u30b7\u30fc\u30d0\u304c\u7a7a\u306e\u30cf\u30c3\u30b7\u30e5\u306e\u5834\u5408<\/li>\n<\/ul>\n<p>\u4e00\u8a00\u3067\u8a00\u3046\u306a\u3089\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u7a7a\u3063\u307d\u306e\u6642\u306b true \u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<p>\u6ce8\u610f\u70b9\u3068\u3057\u3066\u306f\u30013 \u756a\u76ee\u306e\u300c\u30ec\u30b7\u30fc\u30d0\u304c\u30db\u30ef\u30a4\u30c8\u30b9\u30da\u30fc\u30b9\u306e\u307f\u3092\u542b\u3080\u6587\u5b57\u5217\u306e\u5834\u5408\u300d\u3067\u3059\u3002<\/p>\n<p>\u3053\u3053\u3067\u306e\u300c\u30db\u30ef\u30a4\u30c8\u30b9\u30da\u30fc\u30b9\u300d\u306f Unicode \u3067\u5b9a\u7fa9\u3055\u308c\u308b\u30db\u30ef\u30a4\u30c8\u30b9\u30da\u30fc\u30b9\u3001\u3064\u307e\u308a\u5168\u89d2\u30b9\u30da\u30fc\u30b9\u3082\u542b\u307e\u308c\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>present?<\/h2>\n<p>present? \u306f\u5b9a\u7fa9\u3092\u898b\u305f\u65b9\u304c\u65e9\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">def present?\r\n  !blank?\r\nend<\/pre>\n<p>&nbsp;<\/p>\n<h2>presence<\/h2>\n<p>presence \u306f present? \u304c true \u306e\u5834\u5408\u306b\u30ec\u30b7\u30fc\u30d0\u81ea\u8eab\u3092\u8fd4\u3059\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<p>\u30e1\u30bd\u30c3\u30c9\u306e\u5b9a\u7fa9\u3092\u898b\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">def presence\r\n  self if present?\r\nend<\/pre>\n<p>\u5c11\u3057\u5909\u308f\u3063\u305f\u30e1\u30bd\u30c3\u30c9\u306a\u306e\u3067\u4f55\u306b\u4f7f\u3046\u306e\u304b\u3001\u4e00\u898b\u3057\u3066\u5206\u304b\u308a\u3065\u3089\u3044\u3067\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u6bd4\u8f03\u3059\u308b\u3068\u5206\u304b\u308a\u3084\u3059\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">host = config[:host] || 'localhost'\r\nhost = config[:host].presence || 'localhost'<\/pre>\n<p>\u300chost = config[:host] || 'localhost'\u300d\u3068\u3044\u3046\u66f8\u304d\u65b9\u306f\u4e00\u822c\u7684\u306b\u826f\u304f\u898b\u308b\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n<p>host \u306b\u306f config[:host] \u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u5834\u5408\uff08nil \u307e\u305f\u306f false \u3067\u306f\u306a\u3044\u5834\u5408\uff09\u306f config[:host] \u306e\u5024\u3001\u305d\u3046\u3067\u306a\u3051\u308c\u3070 'localhost' \u304c\u4ee3\u5165\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u3082\u3046\u4e00\u65b9\u306e\u300chost = config[:host].presence || 'localhost'\u300d\u306f\u3069\u3046\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>host \u306b\u306f config[:host].present? \u304c true \u306e\u5834\u5408\uff08\u30db\u30ef\u30a4\u30c8\u30b9\u30da\u30fc\u30b9\u306e\u307f\u306a\u3069\u306e\u7121\u610f\u5473\u306a\u5024\u3067\u306f\u306a\u3044\u5834\u5408\uff09\u306b config[:host] \u306e\u5024\u3001\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u300clocalhost\u300d\u304c\u4ee3\u5165\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>presence \u306e\u4f7f\u3044\u3069\u3053\u308d\u306e\u4e00\u3064\u3068\u3057\u3066\u306f\u3001\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3093\u3060\u30cf\u30c3\u30b7\u30e5\uff08\u4e0a\u8a18\u306e\u4f8b\u3067\u306f config\uff09\u304c\u3042\u308a\u3001\u5024\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u9805\u76ee\u306f\u7a7a\u6587\u5b57\u5217\u306b\u306a\u3063\u3066\u3044\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>config[:host] \u304c\u7a7a\u6587\u5b57\u5217\u306e\u5834\u5408\u3001\u300chost = config[:host] || 'localhost'\u300d\u3067\u306f host \u306b\u306f\u7a7a\u6587\u5b57\u5217\u304c\u4ee3\u5165\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u304c\u3001\u300chost = config[:host].presence || 'localhost'\u300d\u3060\u3068 'localhost' \u304c\u4ee3\u5165\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u540d\u8107\u5f79\u3068\u8a00\u3063\u3066\u3082\u904e\u8a00\u3067\u306f\u306a\u3044 ActiveSupport \u306e\u30e1\u30bd\u30c3\u30c9\u3092\u3044\u304f\u3064\u304b\u3054\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u565b\u3081\u3070\u565b\u3080\u307b\u3069\u5473\u304c\u51fa\u308b\u30e1\u30bd\u30c3\u30c9\u3070\u304b\u308a\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>\u4fbf\u5229\u306a\u30e1\u30bd\u30c3\u30c9\u3084\u30a4\u30c7\u30a3\u30aa\u30e0\u306e\u5b58\u5728\u3092\u77e5\u3063\u3066\u3057\u307e\u3063\u305f\u3068\u304d\u306e\u697d\u3057\u3055\u306f\u3001Ruby\/Rails\u3092\u4f7f\u3046\u7406\u7531\u306e\u4e00\u3064\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001\u4fbf\u5229\u30e1\u30bd\u30c3\u30c9\u306e\u5b9d\u5eab ActiveSupport \u304b\u3089\u3044\u304f\u3064\u304b\u3054\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<br \/><a href=\"https:\/\/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\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[68,22,24],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1302"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=1302"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1302\/revisions"}],"predecessor-version":[{"id":17993,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1302\/revisions\/17993"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=1302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=1302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=1302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}