ã¢ããªã±ã¼ã·ã§ã³ã«ã¯ããããè¨å®ããããã®ã§ããä¸è¬çãªä¾ã§ã¯ãproduction / development ã®ãããªã¢ã¼ããããã°ã¬ãã«ãªã©ãããã°ã©ã ãæ¸ãã¨ãããããããã¼ã¿ãã©ã®ããã«ç®¡çãã¾ããï¼
大ãã 2 種é¡ã®æ¸ãæ¹ãããã¨æãã¾ãã
- ã°ãã¼ãã«å¤æ°ï¼ãããã«é¡ãããã®ï¼ã«å ¥ãã
- ã¯ã©ã¹çææã«ã³ã³ã¹ãã©ã¯ã¿ã«å¼ãåã
ã©ã¡ããä¸é·ä¸çãããã¨æã£ã¦ãã¾ãã
1. ã°ãã¼ãã«å¤æ°ã«å ¥ãã
ãã¨ãã°ã$config = { mode: :development }
ãªã©ã¨ããã ãã
Rubyã®å ´åãã°ãã¼ãã«å¤æ°ã ãã§ãªããå®æ°ã«å ¥ããããã¯ã©ã¹ã¡ã½ããã«ããããsingletonã©ã¤ãã©ãªã使ã£ãããã¹ã¬ãããã¼ã«ã«ã¹ãã¬ã¼ã¸ã使ã£ããããã®ããã®åé¡ã«å«ãã¾ãã
é·æ
- è¨è¿°ãã¨ã¦ãç°¡æ½
- è¨å®ã®è¿½å ãç°¡å
çæ
- ã°ãã¼ãã«å¤æ°ã使ããã¨ã«ç½ªæªæããã
- 1 ã¤ã®ããã»ã¹ã§å¯ä¸ã®è¨å®ããæã¦ãªã
解説
ã¨ã«ããç°¡åãªæ¹æ³ãè¨å®æ
å ±ãå¿
è¦ãªã¨ãã¯ã°ãã¼ãã«å¤æ°ãèªã¿åºãã ããªã®ã§ãããã°ã©ã ã®è¨è¿°ã¯é常ã«ç°¡æ½ãã¾ããè¨å®ã追å ããããªã£ãã¨ããèªæãååã®è¡çªãæ°ã«ãªããªãã$MyApp_config
ã®ããã«ãã¢ããªã±ã¼ã·ã§ã³åãprefixããã¨è¯ãããï¼ãã ãé·ããªãã®ã§ãè¨è¿°æ§ã¯è¥å¹²ä¸ããï¼ã
ãã®æ¹æ³ãé¸ã³ãããªãæ大ã®çç±ã¯ãæ°æã¡ã®åé¡ã ã¨æããæè²ãããããã°ã©ãã¯ãã°ãã¼ãã«å¤æ°ã¯ãã¡ãã¨å¿ã«å·ãè¾¼ã¾ãã¦ããããããè¨å®æ å ±ãã°ãã¼ãã«å¤æ°ã«å ¥ãããã¨ã¯ãããã¾ã§åé¡ã§ã¯ãªãæ°ããããã°ãã¼ãã«å¤æ°ããã¡ãªã®ã¯ãå®è¡ä¸ã«ã°ãã¼ãã«å¤æ°ãæ¸ãæãããã¨ã§ã¢ã¸ã¥ã¼ã«éã«æé»çãªä¾åãçºçãã¦ãã¾ãããã ããã©ãè¨å®æ å ±ã¯ã¢ããªåæåæ以å¤ã«ä»£å ¥ããããã¨ã¯ãªãã¯ããªã®ã§ãæå³ããªãä¾åã¯çºçããªãã¯ãã
ãã®æ¹æ³ã®å®ç¨ä¸ã®åé¡ã¯ãè¨å®æ å ±ãããã»ã¹å ã§1ã¤ããæã¦ãªããã¨ãã¤ã¾ããåä¸ããã»ã¹å ã§ç°ãªãè¨å®ã®ã¢ããªã±ã¼ã·ã§ã³ã¤ã³ã¹ã¿ã³ã¹ãè¤æ°ä½ããã¨ãã§ããªãã大ãã¦åé¡ãªãã¨æããããããªããããã¹ãæãªã©ã«æ§ã ãªè¨å®ã§ã®ã¢ããªã±ã¼ã·ã§ã³ã¤ã³ã¹ã¿ã³ã¹ãä½ããããªã£ãããã¢ããªã±ã¼ã·ã§ã³ãã©ã¤ãã©ãªã¨ãã¦ä½¿ããããªã£ããããã¨ãã«å°ãã
ãªããå®æ°ãã¯ã©ã¹ã¡ã½ãããªã©ãæ¬è³ªçã«ã¯ã°ãã¼ãã«å¤æ°ã¨å¤ãããªãã®ã§ãããã»ã¹ã®åé¡ã¯è§£æ±ºããªã *1 ãã¹ã¬ãããã¼ã«ã«ã¹ãã¬ã¼ã¸ã使ã£ã¦ããæ¬è³ªçãªåé¡ã¯å¤ãããªã *2 ã
2. ã¯ã©ã¹çææã«ã³ã³ã¹ãã©ã¯ã¿ã«å¼ãåã
ãã¨ãã°æ¬¡ã®ããã«ãã¯ã©ã¹ãã¨ã«ã¤ã³ã¹ã¿ã³ã¹å¤æ°@config
ãªã©ã§ä¿æãããè¨å®æ
å ±ãå¿
è¦ãªã¯ã©ã¹ã«ã¯å¿
ãè¨å®æ
å ±ã渡ãã
class MyApp::Foo def initialize(config, ...) @config = config ... @bar = Bar.new(@config, ...) ... end end
é·æ
- ã°ãã¼ãã«å¤æ°ãé¿ããæ£ãããã©ã¯ãã£ã¹ãå®è·µãã¦ããæ°åã«ãªãã
- ã¢ããªã±ã¼ã·ã§ã³ã¤ã³ã¹ã¿ã³ã¹ãã¨ã«ç¬ç«ã®è¨å®æ å ±ãæã¦ã
çæ
- è¨è¿°ã極ãã¦åé·ï¼ã»ã¨ãã©ãã¹ã¦ã®ã¯ã©ã¹ã«è¨å®æ å ±ããããåãããå¿ è¦ãããï¼
- ã¤ã³ã¹ã¿ã³ã¹ã大éã«çæããå ´åãé度ãã¡ã¢ãªä½¿ç¨éãããæ°ã«ãªãï¼å¤ãã®å ´åã¯ç¡è¦ã§ããã¨ã¯æãï¼
解説
æç§æ¸çã«æ£ããããªæ¹æ³ãã°ãã¼ãã«å¤æ°ã®é¡ãä¸åãªãã®ã§ç½ªæªæããªããã¾ããç°ãªãè¨å®ã®ã¢ããªã±ã¼ã·ã§ã³ã¤ã³ã¹ã¿ã³ã¹ãä½ããã¨ãã§ããã
ããããè¨è¿°æ§ã¯æããã«ã°ãã¼ãã«å¤æ°ã®æ¹æ³ããå£ããinitialize
ã¯å¿
ã@config
ãåãåãå¿
è¦ããããnew
ã«ã¯å¿
ã@config
ã渡ãå¿
è¦ãããã
ã¾ããASTã®ãã¼ãã®ã¯ã©ã¹ã®ããã«ãã¤ã³ã¹ã¿ã³ã¹ã大éçæãããå¯è½æ§ãããã¯ã©ã¹ã«æ¯å@config
ãããããã®ã¯ãç¡é§ãæ°ã«ãªããªãããªããã»ã¨ãã©ã®å ´åããã¹ã¦ã®@config
ã«ã¯åããªãã¸ã§ã¯ãã¸ã®åç
§ãå
¥ãã ããªã®ã§ãï¼ãã ããå®éã«åé¡ã«ãªãå¯è½æ§ã¯é«ããªãã¨æãï¼
è¿ãå°æ¥ã«ã¢ããªã±ã¼ã·ã§ã³ã¤ã³ã¹ã¿ã³ã¹ãè¤æ°ä½ãå¿ è¦ããªãå ´åããã®æ¹æ³ãé¸ã¶ã¡ãªããã¯ãªãã¨æããããããå¿ è¦ã«ãªã£ã¦ããæ¸ãæããã«ã¯ãå¤æ´éã¯é常ã«å¤ãã¦ã¤ãããããããã ããã¨ãã£ã¦é²è¡çã«ãã£ã¡ã®æ¹æ³ãé¸ãã§è¨è¿°æ§ãå®è¡å¹çãä¸ããã®ã¯ãçç£æ§ãä¸ãã¦ããã¨æãã¦ãã¾ãâ¦â¦ã
ãªãããã®æ¹æ³ã®ããªã¢ã³ãã¨ãã¦ãããã¹ã¦ã®è¨å®æ å ±ã渡ãã®ã§ã¯ãªãããã®ã¯ã©ã¹ãå¿ è¦ã¨ãã¦ããæ å ±ã ãã渡ããã¨ããã®ããããããã¯ãããæ£ãããæããããããçç£æ§ã¯ããææªã«ãªã *3 ã
ã©ããªãã°å¹¸ãã
ä¸ã® 2 ã¤ã¯ã©ã¡ããé¸ãã§ãä½ãééã£ã¦ãããããªæ°åã«ãªãããã¤ãã¢ã¤ã¢ã¤ãã¦ãã¾ãããªã«ãè¨èªæ©è½ãä¸è¶³ãã¦ããã®ã§ã¯ãªããã¨æãã®ã§ãããã©ãããæ©è½ãããã°ç¶ºéºã«è§£æ±ºããã®ãããããããã¾ããã
- åçã¹ã³ã¼ããªå¤æ°ãããã°ããã®ã§ã¯ï¼ã¨æã£ã¦ããææããã£ãã®ã§ãããè¤æ°ã®ã¢ããªã±ã¼ã·ã§ã³ã¤ã³ã¹ã¿ã³ã¹ãç¸äºã«å®è¡ãããããªå ´åã«ã¯ã¤ãã¤ã使ããªãããã
- ãªãã¸ã§ã¯ããã°ã«ã¼ãåããæ©è½ãããã°ãããããªæ°ããã¾ãããè¨è¿°æ§ãä¸ããã«æå±ã°ã«ã¼ããæå®ããæ¹æ³ãæãã¤ããªããRactor ãè¿ããã ããã©ãRactor ã¯ãã¯ãå¥ç©ãªã®ã§å¤å転ç¨ã¯ãããããï¼ã·ã§ã¢ããããªãã¸ã§ã¯ãã®ä¸ã§è¨å®ãåç §ããã¨ææ§ã¨ãï¼ã
ã¾ã¨ã¾ããªãã¾ã¨ã
ããããããããªã®ã§ãè¨å®æ å ±ãã§è©±ãã¾ããããã¢ããªã±ã¼ã·ã§ã³å ¨ä½ã®ç®¡çãè¡ãï¼åã°ãã¼ãã«ãªï¼ã¤ã³ã¹ã¿ã³ã¹ãåæ§ã«ãã¹ã¦ã®é¢ä¿ã¤ã³ã¹ã¿ã³ã¹ã«åç §ãããããæ°æã¡ã«ãªãã¾ãã
ãã®åé¡ãã¨ã¬ã¬ã³ãã«è§£æ±ºãã¦ããè¨èªã®äºä¾ã¨ãè¨èªæ©è½ã®è«æã¨ãããã°ãæãã¦ãã ããã
*1:ãã°ãã¼ãã«å¤æ°ãã¨ããè¨èã¯é¿ããããã®ã§ãæ°æã¡ã¯è½ã¡çããããããªããå®éãå®è¡ä¸ã«ãã£ããæ¸ãæãã¦ãã¾ããªã¹ã¯ã¯æ¸ãã®ã§ãå¤å°æ¹åã¯ãã¦ããã
*2:ãã¹ã¬ããå 㧠1 ã¤ãã«ç·©åã§ãããã®ã®ãã¢ããªã±ã¼ã·ã§ã³ã¤ã³ã¹ã¿ã³ã¹ãè¤æ°åå± ãããããªãåé¡èªä½ã¯å¤ãããªãããã¨è¨è¿°ãé·ããªããã¡ã
*3:ãã¯ã©ã¹ãå¿ è¦ã¨ãã¦ããæ å ±ãã¯ãªããªãäºæ¸¬ã§ããªããããã®ã¯ã©ã¹èªèº«ãå¿ è¦ã¨ããªãã¦ãããã®ã¯ã©ã¹ã®ä¸ã§å¥ã®ã¯ã©ã¹ãã¤ã³ã¹ã¿ã³ã¹åããéã«å¿ è¦ã«ãªãã¨ãããã¨ãããããã