é¢æ°ããã°ã©ãã³ã°ã£ã½ãmapã«2å¼æ°é¢æ°ãã«ã¼ãªã¼åãã¦æ¸¡ã
rubyã§1å¼æ°é¢æ°ãmapã«æ¸¡ãå ´åã¯ããããªãããã§ã¡ã½ããã®ã·ã³ãã«ãProcãªãã¸ã§ã¯ãã¨ãã¦æ¸¡ãã¦é¢æ°ããã°ã©ãã³ã°ã£ã½ãæ¸ãæ¹ãã§ãããã ãã©ã
irb > ('a'..'c').map &:upcase => ["A", "B", "C"]
2å¼æ°ã«ãªãã¨ã©ãæ¸ãã°ããããããã¨æã£ã¦ãã®ã ãã©ããã¼ãã§ã¯ãRubyèªãã§ããã9.3ç« Methodãªãã¸ã§ã¯ãã¨Procãªãã¸ã§ã¯ãã®ã¨ããã«ãããªæ¸ãæ¹ãè¼ã£ã¦ãã
irb > (0..10).map &2.method(:*).to_proc => [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
ããããç´æçãããªããã©ãæ°åã®2ã®ã¤ã³ã¹ã¿ã³ã¹ããä¹ç®ã¡ã½ãã*ã®Methodãªãã¸ã§ã¯ããåå¾ãã¦ãProcãªãã¸ã§ã¯ãã«å¤æãã¦æ¸¡ãã¦ããæ°åã®2ã¯Mehtodãªãã¸ã§ã¯ãã®ã¬ã·ã¼ãã¨ãã¦æç¸ããã¦ãã®ã§ãã«ã¼ãªã¼åãããããã«ãªã£ã¦ãã
ã¡ãªã¿ã«æå¾ã®to_procã¯çç¥ã§ããã®ã§ã以ä¸ã®æ¸ãæ¹ã§ãOKã ã£ãã
irb > (0..10).map &2.method(:*) => [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
ããã£ï¼Procãªãã¸ã§ã¯ãã«å¤æã§ãããã®ãªããããã¨ãããã¨ã¯ãããlambdaã§ããããããããï¼ã¨æã£ã¦è©¦ãã¦ã¿ããåºæ¥ãã
irb > (0..10).map &->(x){2*x} => [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
ã ãã¶ç´æçãªãããã«ãªã£ããã¡ãªã¿ã«è©¦ããã®ã¯ruby2.2.0ã§ãã