ããã°ã©ãã³ã°ã®åå¼·ããã¦ããã¨ãç°ãªã話é¡ãªã®ã«ä¼¼ããããªååãä»ãããã¦ãã¦æ··ä¹±ãããã¨ãããã
ä¾ãã°å¤æ°ã«ããããåç §åãã¨é¢æ°ï¼ããã·ã¼ã¸ã£ï¼ãµãã«ã¼ãã³ï¼ã¡ã½ããçã ã®å¼æ°ã«ããããåç §æ¸¡ããã¯ç°ãªãæ¦å¿µãªã®ã ããã©ã¡ãããåç §ãã¨ããååãä»ãã¦ããçºã«これã®ããã«æ··ä¹±ãã¦ãã¾ããã¨ãããã
ã¾ããã®è¾ºãã¯ç¥ããªãã¨è²ã ã¨æ··ä¹±ããããã話ã§ã¯ããã
åãããªã人ã¯VBScriptãC#ã®è¨èªæ¬ä½ã®é¨åãåå¼·ããã¨ç解ã§ãããããããªããã©ã¡ããã
- å¤æ°ï¼ã¨ããããã¼ã¿åï¼ï¼ã«å¤åã¨åç §åãããã
- ã¡ã½ããã®å¼æ°ã«å¤æ¸¡ãã¨åç §æ¸¡ãï¼å¤æ°æ¸¡ãï¼ãããã
ã¨ãã£ãè¨èªä»æ§ã ã
ã¡ãªã¿ã«ç§ã¯åºæ¬çã«Cããã°ã©ããªã¹ã¿ã³ã¹ã®äººãªã®ã ããCè¨èªã®ä»æ§ã¨ãã¦ã¯ã
- å¤æ°ã«å¤åï¼æ®éã®å¤æ°ï¼ã¨åç §åï¼ã®åå§çãªå®è£ ââãã¤ã³ã¿ï¼ãããã
- é¢æ°ã®å¼æ°ã¯å¤æ¸¡ãã®ã¿ã
ã¨ãã£ãç¹å¾´ãããã®ã§ãå®ã¯Rubyã®å¤æ°ã¨ã¡ã½ããã®å¼æ°ãããã話ã¯çµæ§ç解ããããã£ããããã
ã©ããããã¨ãï¼ãRubyã®ã¡ã½ããã®å¼æ°ã¯å¤æ¸¡ãã§ãRubyã®å¤æ°ã¯å ¨ã¦ãªãã¸ã§ã¯ããæã示ããã¤ã³ã¿ã«ä¼¼ãä½ãã ã¨èããã°ããã®ã *1ã
ãããªãµã³ãã«ã³ã¼ããã§ã£ã¡ä¸ãã¦ã¿ãã
#!/usr/bin/ruby -w def test1(s) s = "bar" end def test2(s) s.reverse! end str = "foo" test1(str); p str # "foo" ã¨è¡¨ç¤ºããã test2(str); p str # "oof" ã¨è¡¨ç¤ºããã
test1ã®ãs = "bar"ãã¯ãå¤æ°sããã¤ã³ããã¦ãããªãã¸ã§ã¯ããå¤æ´ãã¦ããããsèªä½ã¯strã®å¤ï¼ã©ã®ãªãã¸ã§ã¯ãããã¤ã³ããã¦ãããã¨ããæ å ±ï¼ãå¤æ¸¡ãã§ã³ãã¼ãããã®ã«ãããªããstrããã¤ã³ããã¦ãããªãã¸ã§ã¯ãæ¬ä½ã¯å¤æ´ããã¦ããªãã®ã§ããp strãã§è¡¨ç¤ºãããã®ã¯ã"foo"ãã®ã¾ã¾ã ã
# ç¢å°ã¯ã©ã®ãªãã¸ã§ã¯ããåç §ãã¦ãããã表ãã # {}ã¯ãªãã¸ã§ã¯ããèå¥ç¨ã®idï¼å¤ã¯é©å½ï¼ã¨å®éã®å¤ãæ ¼ç´ããvalãæã¤ã # ï¼Rubyæ¬æ¥ã®ãªãã¸ã§ã¯ãã§ã¯ãªãã便å®ä¸ç¨æããæ¶ç©ºã®ãªãã¸ã§ã¯ãï¼ # s = "bar" ãå®è¡ããç´åã # str ã s ãåããªãã¸ã§ã¯ããæã示ãã¦ããã str --+--> { id: 1, val: "foo" } | s ----+ # s = "bar" ãå®è¡ããç´å¾ã # s ãéããªãã¸ã§ã¯ããæã示ãããã«ãªã£ãã str -----> { id: 1, val: "foo" } s -------> { id: 2, val: "bar" }
test2ã®ãs.reverse!ãã¯ãå¤æ°sããã¤ã³ããã¦ãããªãã¸ã§ã¯ãã®å é¨ç¶æ ãå¤æ´ãã¦ãããsããã¤ã³ããã¦ãããªãã¸ã§ã¯ãã¨strããã¤ã³ããã¦ãããªãã¸ã§ã¯ãã¯åããªã®ã§ããp strãã§ã®è¡¨ç¤ºçµæã«ãå¤æ´ãåæ ãããã
# s.reverse! ãå®è¡ããç´åã # str ã s ãåããªãã¸ã§ã¯ããæã示ãã¦ããã str --+--> { id: 1, val: "foo" } | s ----+ # s.reverse! ãå®è¡ããç´å¾ã # str ã s ãæã示ããªãã¸ã§ã¯ãã¯å¤ãã£ã¦ããªããã # åç §å ã®ãªãã¸ã§ã¯ããç ´å£çæä½ããçºãå é¨ç¶æ ãå¤åãã¦ããã str --+--> { id: 1, val: "oof" } | s ----+
ãã®Rubyã®ã¹ã¯ãªããã¨ä¼¼ããããªå 容ãC++ï¼better Cï¼ã§æ¸ãã¨ãããªãã
#include <algorithm> #include <iostream> #include <string> static void test1(std::string *s) { s = &std::string("bar"); } static void test2(std::string *s) { std::reverse(s->begin(), s->end()); } int main() { using std::string; using std::cout; using std::endl; // new ã«å¤±æããå¯è½æ§ã¯ããã¦ç¡è¦ string *str = new string("foo"); test1(str); cout << *str << endl; // "foo" ã¨è¡¨ç¤ºããã test2(str); cout << *str << endl; // "oof" ã¨è¡¨ç¤ºããã delete str; return 0; }
C++ã®åç §ã§ã¯ãªããã¤ã³ã¿ã使ã£ã¦ããç¹ã«æ³¨æãããã¯æå³ãã¦æ¸ãã¦ããã
test1ã®å¼æ°sã¯ãã¤ã³ã¿ã ãtest1ã®ä¸ã§sã®å¤ï¼string("foo")ï¼ã¸ã®ã¢ãã¬ã¹ï¼ãå¤æ´ãã¦ããããsãå ã æãã¦ãããªãã¸ã§ã¯ãèªä½ãå¤æ´ãã訳ã§ã¯ãªããstrããã¤ã³ããã¦ãããªãã¸ã§ã¯ãã¯ä½ãå¤æ´ããã¦ããªãã®ã§ããcout << *str << endlãã§è¡¨ç¤ºãããã®ã¯ã"foo"ãã®ã¾ã¾ã ã
ãã®ä»£ããtest2ã®ããã«ãã¤ã³ããã¦ãããªãã¸ã§ã¯ãã®å é¨ç¶æ ãå¤æ´ãã¦ãã¾ãã°ããcout << *str << endlãã§ã®è¡¨ç¤ºçµæãå¤æ´ãåæ ããããã®ã«ãªãã
ï¼æ§æããèªã¿åãããã¥ã¢ã³ã¹ã¨ãã¦ã¯ï¼Rubyã®å¤æ°ã¯åç §åã ããªãã¸ã§ã¯ãæ¬ä½ã§ã¯ãªãããªãã¸ã§ã¯ããæã示ãä½ãã ãCè¨èªã«ä¾ããã¨ãã¤ã³ã¿ã«è¿ããä½ãCè¨èªã®ãã¤ã³ã¿ã¨ç°ãªãã¤ã³ã¯ãªã¡ã³ãï¼ãã¯ãªã¡ã³ããã¢ãã¬ã¹å¤ã¸ã®æ¼ç®ã¯ã§ããªãããæ§æä¸ãã¤ã³ã¿ã§ãããã¨ãæ°ã«ããå¿ è¦ã¯ãªã*2ã
Rubyã«ãããå¤æ°ã¸ã®ä»£å ¥ã¯ãåã«æã示ããªãã¸ã§ã¯ããå¤æ´ãã¦ããã«éããªããCè¨èªã§è¨ããªããã¤ã³ã¿ã®å¤ï¼ã¢ãã¬ã¹å¤ï¼ãå¤ãã¦ãããããªãã®ã ã
ããã¦Rubyã®ã¡ã½ããã®å¼æ°ã¯å¤æ¸¡ãã ãCè¨èªã«ä¾ããã¨ãã¤ã³ã¿ã®å¤ï¼ã¤ã¾ãã¢ãã¬ã¹å¤ï¼ãé¢æ°ã®å¼æ°ã«ã³ãã¼ãã¦ãã訳ã ããã¤ã³ãå ã®ãªãã¸ã§ã¯ããç´æ¥å¼ããã°ããã®ãªãã¸ã§ã¯ããæã示ãã¦ããä»ã®å¤æ°ã«ãå½±é¿ãåºã*3ããããã¡ã½ããå ã§å¼æ°ã«ä»ã®å¤ãä»£å ¥ãã¦ããã®ãã¤ã³ã¿ãæã示ããªãã¸ã§ã¯ããå¤ããã ãã ãå¼æ°ã«æå®ããå ã®å¤æ°ãæã示ãå ãå¤ããããã§ã¯ãªãããã¾ãã¦ãæã示ããªãã¸ã§ã¯ãã®å é¨ç¶æ ãå¤åããããã§ããªãã
å¤æ°ã®åç §åã¨å¼æ°ã®åç §æ¸¡ãã¯ãã©ã¡ãããåç §ãã¨ããè¨èã使ã£ã¦ããããå ¨ãå¥ç©ã ãä¾ãããªããã«ãã¤ã³ã¿ã¨ãã«æåãããã«éãã
æåã«ãæ¸ãããããã®ç¹ãææ§ãªäººã¯VBScriptããããåå¼·ãã¦ã¿ãã¨è¯ãã¨æã*4ãVBScriptã¯ããã·ã¼ã¸ã£ã®å¼æ°ã«ByValï¼å¤æ¸¡ãï¼ãªããByRefï¼åç §æ¸¡ãï¼ãã¼ã¯ã¼ããæå®ã§ãã*5ãã¨åæã«ããªã¢ã³ãåã®å¤æ°ã®ä¸èº«ããªãã¸ã§ã¯ãåï¼ãªãã¸ã§ã¯ããåç §ãããã¼ã¿åï¼ãªã®ããã以å¤ãªã®ãã«ãã£ã¦ä»£å ¥æã«Setãå¿ è¦ï¼ä¸è¦äºã ã®è©±ããã訳ã§ãå¼æ°çµ¡ã¿ã§é½å (2 * 2) == 4 ãã¿ã¼ã³ãåå¨ããã¨ãããæ £ããªãã¨æ··ä¹±ããããªç¶æ³ã楽ãããã¨ãã§ããã
- ãªãã¸ã§ã¯ãåã§ã¯ãªãå¤ãå¤æ¸¡ã
- ãªãã¸ã§ã¯ãåã®å¤ãå¤æ¸¡ã
- ãªãã¸ã§ã¯ãåã§ã¯ãªãå¤ãåç §æ¸¡ã
- ãªãã¸ã§ã¯ãåã®å¤ãåç §æ¸¡ã
*1:å°ãªãã¨ãå¤æ°ã«ã¤ãã¦ã¯ãåãã¦ã®Rubyãåçã®P.97ã«ãå¤æ°ãä¿æãããã®ã¯ãªãã¸ã§ã¯ãã¸ã®åç §ã§ãããªãã¸ã§ã¯ããã®ãã®ãå¤ãã®ãã®ãæ ¼ç´ãããããã§ã¯ããã¾ããããããä½ãæå³ãããã¨ããã¨ãè¤æ°ã®å¤æ°ãåããªãã¸ã§ã¯ããåç §ãã¦ããå¯è½æ§ãããã¨ãããã¨ã§ãããã¨æ¸ããã¦ããã
*2:CãC++ã§ãã°ãã°å¿ è¦ã¨ãªãã¡ã¢ãªã®æå解æ¾ãä¸è¦ã ã
*3:CãC++ã®ããã«ãªãã¸ã§ã¯ãèªä½ãã³ãã¼ããè¨èªã«æ £ãã¦ãã人ãRubyã§èºãè¦å ã®1ã¤ã ããã
*4:ç¹ã«Windows使ãã®äººãªãWindows 2000以éã§ããã©ã«ãã§ä½¿ãããPerlãRubyã»ã©ã§ã¯ãªããã©Windowsç¨ã®LL代ããã«ããªãã
*5:çç¥ããã¨åç §æ¸¡ãã«ãªãã