Rubyã«ããã¦ï¼ã||=ãæ¼ç®åã使ããã¨ã®æ義ã¯åãããï¼èªåã§ããã使ãã®ã§ããâ¦
ä»äººã®ã³ã¼ããè¦ãRubyåå¿è ã®å¤ãã¯ï¼ãã®é¢¨å¤ãããªã¤ãã£ãªã ã«å°æããï¼
a ||= []ããã¯ï¼aã®å¤ã空ã®é åã«ãªããããããªãã¨ããæå³ã ï¼||=ã¯ä»¥ä¸ãçç¥ããæ§æã§ããï¼
a = a || []ãã®ã³ã¼ããç解ããã«ã¯ï¼ãã®ORæ¼ç®åï¼||ï¼ã«ã¤ãã¦è©³ããç解ããå¿ è¦ãããï¼||æ¼ç®åã¯ï¼è¢«æ¼ç®åã®ãããããtrueã§ããã°ï¼trueãè¿ãï¼
nilã¨false以å¤ã®å¤ã¯ãã¹ã¦trueã«ãªããã¨ãè¦ãã¦ããã¦ã»ããï¼æåã®è¢«æ¼ç®åãtrueãªãï¼||æ¼ç®åã¯trueãè¿ãï¼falseãªãï¼2çªç®ã®è¢«æ¼ç®åãè¿ãï¼ããã¯ã¤ã¾ãï¼è¢«æ¼ç®åãã©ã¡ããfalseã§ãªãéãï¼çµæã¯trueã«ãªãã¨ãããã¨ã ï¼ããã¯ï¼ããããORæ¼ç®åã®èãã¨åãã ï¼
ã¨ãããã¨ã¯ï¼å ã»ã©ã®ã³ã¼ãã¯ä»¥ä¸ã®ã³ã¼ãã¨åãã ã¨èããããï¼if a != nil a = a else a = [] endãã®ã³ã¼ãã¯ä»¥ä¸ã®ããã«ç¿»è¨³ã§ããï¼ãããaãnilã§ãªããªãã°ï¼ãã®ã¾ã¾ã«ããï¼nilãªãã°ï¼ç©ºã®é åã«ããï¼ãçç·´Rubyããã°ã©ãã¯ï¼ifããã||æ¼ç®åã®ã»ããåªé ã§èªã¿ãããã¨èããããã ï¼ãã®ã¤ãã£ãªã ã¯é å以å¤ã«ã使ããï¼å¤æ°ãnilã«ãªããªãããã«ããã¬ã¼ãï¼è¦å¼µãå½¹ï¼ãªã®ã§ï¼ãã®ã¤ãã£ãªã ã¯nilã¬ã¼ãã¨å¼ã°ããï¼
nilã¬ã¼ãã¯ã¤ã³ã¹ã¿ã³ã¹å¤æ°ãåæåããã®ã«ããã使ãããï¼(ç¥)
ï¼ã¡ã¿ããã°ã©ãã³ã°Ruby*1, p.268ï¼
ä¸ã®èª¬æï¼ééããå¤æ°ï¼ç®ã«ã¤ãã¦å°ãã¾ãï¼
- ãa ||= []ã*2ã¯ãa = a || []ãã§ã¯ãªãï¼ãa || (a = [])ãã§ãï¼ãªãã¡ã¬ã³ã¹ããã¥ã¢ã«ã®ãæ¼ç®åå¼ãã§ç¢ºèªã§ãã¾ã*3ï¼
- ã1 || trueãã¯trueã§ã¯ãªã1ã¨è©ä¾¡ããã¾ã*4ï¼ããã¯ï¼ã||æ¼ç®åã¯ï¼è¢«æ¼ç®åã®ãããããtrueã§ããã°ï¼trueãè¿ããã¨ã被æ¼ç®åãã©ã¡ããfalseã§ãªãéãï¼çµæã¯trueã«ãªããã«å¯¾ããåä¾ã«ãªãã¾ãï¼
- ãnilã¨false以å¤ã®å¤ã¯ãã¹ã¦trueã«ãªããã£ã¦ï¼ãããªãã¨ã¯ããã¾ããï¼ããã®ãtrueãã¯ãçããªã®ã§ãããï¼å ¨ä½çã«ï¼ãå®æ°trueãã¨ãçã¨ãªãå¤ãã®åºå¥ãã¤ãã¦ããªãå°è±¡ãããã¾ãï¼
- ifã使ã£ã代æ¿ã³ã¼ãã®ãif a != nilãã¯ãif a != nil && a != falseãã¾ãã¯ãif aãã¨ãï¼æ¬¡ã®è¡ã®ãa = aãã¯åã«ãaãã¨ãã¹ãã§ãï¼
è¨èªä»æ§ã¨ãã¦ï¼ãa ||= []ãã¯ãa = a || []ãã§ã¯ãªããa || (a = [])ãã¨ãªã£ã¦ããã®ã§ããã¾ã§ãªã®ã§ããï¼ç¢ºèªã®ã³ã¼ããæ¸ãã¦ã¿ã¾ããï¼
#!/usr/bin/env ruby # -*- coding: utf-8 -*- # sample.rb class Sample def initialize @x = nil end def x puts "ã¤ã³ã¹ã¿ã³ã¹å¤æ°@xãåç §ãã¾ãã" @x end def x=(value) puts "ã¤ã³ã¹ã¿ã³ã¹å¤æ°@xã«#{value}ãä»£å ¥ãã¾ãã" @x = value end end if __FILE__ == $0 s = Sample.new puts "s.x ||= [] 1åç®" s.x ||= [] puts "s.x ||= [] 2åç®" s.x ||= [] puts "s.x = s.x ã ã¨?" s.x = s.x end
ãå¤æ°aã®å¤ãåç
§ããããã¨ããå¤æ°aã«ä»£å
¥ãè¡ãããã¨ãæ¤åºãããããªï¼Rubyã®ã³ã¼ãã¨ããã®ã¯ï¼ã¡ãã£ã¨é£ãããã§ãï¼ã¾ãï¼=ã¨||ã¯åå®ç¾©ã§ããªãæ¼ç®åã§ããç¹ã«ãï¼çæããªãã¨ããã¾ããï¼ã§ããï¼ç¬èªã«å®ç¾©ããã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹sã«å¯¾ãã¦ï¼ãs.xã®å¤ãåç
§ããããã¨ããs.xã«ä»£å
¥ãè¡ãããã¨ã¯ã¡ã½ããã¨ãã¦è¨è¿°ãã§ãï¼ãã®ä¸ã«putã«ããåºåå¦çãæ¸ãã¦ããã°ï¼åç
§ã»ä»£å
¥ãã©ã®ã¿ã¤ãã³ã°ã§ãªããããï¼ç®ã«è¦ãã¦åããã¨ãã次第ã§ãï¼
ä¸ã®ããã°ã©ã ã§ï¼å®è¡åã«èãããã¨ã¯æ¬¡ã®2ã¤ã§ãï¼
ãs.x ||= []ãããs.x = s.x || []ãã¨ç価ã§ããã°ï¼
s.x ||= [] 1åç® ã¤ã³ã¹ã¿ã³ã¹å¤æ°@xãåç §ãã¾ãã ã¤ã³ã¹ã¿ã³ã¹å¤æ°@xã«[]ãä»£å ¥ãã¾ãã s.x ||= [] 2åç® ã¤ã³ã¹ã¿ã³ã¹å¤æ°@xãåç §ãã¾ãã ã¤ã³ã¹ã¿ã³ã¹å¤æ°@xã«[]ãä»£å ¥ãã¾ãã s.x = s.x ã ã¨? ã¤ã³ã¹ã¿ã³ã¹å¤æ°@xãåç §ãã¾ãã ã¤ã³ã¹ã¿ã³ã¹å¤æ°@xã«[]ãä»£å ¥ãã¾ãã
ã¨ããåºåã«ãªãã¾ãï¼
ããã§ã¯ãªãï¼ãs.x ||= []ãããs.x || (s.x = [])ãã¨è§£éãããã®ãªãï¼
s.x ||= [] 1åç® ã¤ã³ã¹ã¿ã³ã¹å¤æ°@xãåç §ãã¾ãã ã¤ã³ã¹ã¿ã³ã¹å¤æ°@xã«[]ãä»£å ¥ãã¾ãã s.x ||= [] 2åç® ã¤ã³ã¹ã¿ã³ã¹å¤æ°@xãåç §ãã¾ãã s.x = s.x ã ã¨? ã¤ã³ã¹ã¿ã³ã¹å¤æ°@xãåç §ãã¾ãã ã¤ã³ã¹ã¿ã³ã¹å¤æ°@xã«[]ãä»£å ¥ãã¾ãã
ã§ãï¼å®éã«ï¼ããã¤ãã®ç°å¢ï¼1.8ï¼1.9ï¼ã§å®è¡ãã¦ã¿ãã¨ï¼åºåã¯ãã¹ã¦å¾è ã§ããï¼
*1:http://ascii.asciimw.jp/books/books/detail/978-4-04-868715-7.shtml
*2:ã¯ã¦ãªãã¤ã¢ãªã¼è¨æ³ã¨è¡çªããã®ã§ï¼å°ã®æã§ã¯ï¼ãã©ã±ããè¨å·ããã«ããã¤ãæåã§è¡¨ç¾ãã¦ãã¾ãï¼
*3:http://doc.okkez.net/static/192/doc/spec=2foperator.html, http://doc.okkez.net/static/187/doc/spec=2foperator.html, http://www.ruby-lang.org/ja/man/html/_B1E9BBBBBBD2BCB0.html
*4:ã2||1ãã¯ï¼Rubyã§ã¯2ã§ï¼Cã§ã¯1ã§ãï¼Cã®å ´åã¯ï¼è«çORæ¼ç®åã®è©ä¾¡çµæã¯0ã1ã®ããããã«ãªãããã§ãï¼ã2|1ãã¯ï¼Rubyã§ãCã§ã3ã§ãï¼ã¨ãã«|ã¯ï¼ãããORæ¼ç®åã ããã§ãï¼