Rubyã«ã¯ nil
ããããSmalltalkããç¶æ¿ããç¥æµã®1ã¤ã§ããã
ä»æ´Rubyistã«ã¯è¨ãã¾ã§ããªãããããã¯ãã¯ãã«éããªãCã® NULL
ããªãã¸ã§ã¯ãã§ã¯ãªãJavaã® null
ã¨ã¯ä¸ç·ãç»ããã®ã ãC#ã®ä¸ã®äººã null
ãåçãã¦ã nil
ãããªããã¨è¨ã£ã¦ãããããããã©ããã¨ã«ãã nil
ã¯è¯ããã®ã ã
ããã¯ããã©ã«ãã®Null objectã§ãããRubyã«ããã¦ãNull objectãã¿ã¼ã³ã¯å¥å¨ã ãããä½ããã®åºåã§ãããªãã¸ã§ã¯ããã§ããç¨åº¦ã®è»½éãªã±ã¼ã¹ã«ããã¦ã¯ãããããNull objectãå®ç¾©ããªãã¨ãããã©ã«ãã®Null objectã¨ã㦠nil
ã使ããã¨ãã§ãããã ããããã®æå³ã§ã¯Rubyã§ã¯Null objectãã¿ã¼ã³ã¯ãå½ããåãã§ããã¨è¨ã£ã¦ããããããããªãã
空ã»ãã³ãã£ã¯ã¹
Null objectã¯ä½ããã®æå³ã§ããã«ããã¹ããªãã¸ã§ã¯ãã®ä¸å¨ã表ããã»ãã³ãã£ã¯ã¹ã®æå³ã§ä¸å¨ã§ã¯ããã®ã ããã©ããè¨èªã¬ãã«ã§ã¯ããã«ãªãã¸ã§ã¯ããåå¨ãã¦ãããã ããããªãããã®æ©æ§ãéãã¦çæããã¦ãããªãã¸ã§ã¯ããæ±ãã¯ã©ã¤ã¢ã³ãå´ã³ã¼ãã¯ãå¤æ æ§ãéãã¦ç©ºã空ã§ãªãããåããã«ééçã«ãã®åç §å¯¾è±¡ãæä½ã§ããããç¡ã®åå¨ãããªãã¸ã§ã¯ãã¨ãã¦è¡¨ç¾ããããããNull objectãã¿ã¼ã³ã§ããã
ã¨ããã§ã空ã§ãããã¨ã®ã»ãã³ãã£ã¯ã¹ã¯ã±ã¼ã¹ã«ãã£ã¦ç°ãªããããå ´åã«ã¯ nil
ãããå¯ä¸ã®ç©ºç¶æ
ã ãããããå ´åã«ã¯ç¬èªã®Null objectãå¿
è¦ãããããªããããå ´åã«ã¯ã空æååã¯ç©ºãªãã¸ã§ã¯ãã§ããã¹ããããããªããããããã±ã¼ã¹ã§ãä»»æã®ãªãã¸ã§ã¯ãã«å¯¾ãã¦ç¹å®ã®ã»ãã³ãã£ã¯ã¹ã«ããã"空"æ§ãåãåããããããã®ã¨ããRubyã®ãªã¼ãã³ã¯ã©ã¹ã¯æå¹ã«æ©è½ãã¦ãã¤ã¾ãããã®"空"æ§ã表ãè¿°èªã¡ã½ãããä½ãã°ãããActiveSupportã® Object#blank?
ã¯webã¢ããªã±ã¼ã·ã§ã³ã«ãããã空ã®ã»ãã³ãã£ã¯ã¹ã®å®è£
ã§ãããé©ç¨åéã«å¿ãã¦ãè¨èªæ¬ä½ããã¯åãé¢ãã¦ããããå®è£
ã使ããããè¡ããã¨ãã§ããã®ã¯ç´ æ´ãããããããç§ã blank?
ãè³è³ããçç±ã§ãããRubyæ¬ä½ã«åãè¾¼ããã¨ã«å対ãç¶ãã¦ããçç±ã§ããã
空è£å®æ§æ
Rubyã®è«çåæ¼ç®åã¯ãCè¨èªã®ç³»èã«å±ãå¤ãã®è¨èªã¨åããç絡ãããæ´ã«ãRubyã®è«çåæ¼ç®åã¯ãªãã©ã³ãã®ãããããè¿ããæ´ã«å¯¾å¿ããèªå·±ä»£å ¥æ¼ç®åãæã¤ãããããã¤ãã®ã¤ãã£ãªã ãç£ãã
obj ||= default_value
ããã¯ã obj
ãRubyã®çå½è©ä¾¡ã®æå³ã§å½ã§ããå ´åã«ã¯ããã©ã«ãå¤ã§ç½®ãæãããããªãã°ä½ãããªãã¨ãããã¨ãç°¡æ½ã«è¨è¿°ããé »åºã®ã³ã¼ãã§ãããããã§ãRubyã®çå½è©ä¾¡ã«ããã¦ã¯ nil
㨠false
ã®ã¿ãå½ã§ä»ã®å
¨ã¦ã®ãªãã¸ã§ã¯ãã¯çã ãã ããã false
ã®åå¨ãç¡è¦ããã°ãã®ã¤ãã£ãªã ã¯ã nil?
ã®æå³ã§ç©ºã§ããã°è£å®ãããããªãã°ä½ãããªãã¨ãããã¨ãæå³ããã
空è£å®æ§æã®ãªã¼ãã¼ãã¼ã
空è£å®æ§æã®ã»ãã¯ãæ®å¿µãªãã"空"æ§ã«ä»ã®ã»ãã³ãã£ã¯ã¹ãä¸ããæ¹æ³ã¯ãªããã ããã blank?
ã®æå³ã§ç©ºã§ããå¤ãè£å®ããããã°æ¬¡ã®ããã«æ¸ãã»ããªãã
obj = default_value if obj.blank?
ã¾ã¼ãããã§ããããªãã«ç°¡æ½ã¨ãããªãã¯ãªãããã©ãã ||=
ã«æ
£ããè³ã«ã¨ã£ã¦ãè¡ã£ã¦ããã®ã¯ä½ããã®ãé空æ§ä¿è¨¼ãã¨ããå¥ç´(Design by contractã®æå³ã§)ã§ãã£ã¦ãæ¡ä»¶åå²ã§ã¯ãªãããã®ã®ã£ãããæèãä¸ç¬å¦¨ããã
"空"æ§ã®ã»ãã³ãã£ã¯ã¹ãæå®ã㦠||=
ã使ãããã
obj.blank ?= default_value obj ?blank|= default_value obj |blank? = default_value
ãã¾ãã¡è¯ãæ§æç³ãæãã¤ããªããæãã¤ãããruby-devã«ææ¡ãããã®ã ããã©ãã