eachãeach_with_objectãinjectãmap
Ruby ã® eachãeach_with_objectãinjectãmap ã¯ä½¿ãã©ãããå¾®å¦ã«éãã
ããããé©åãªç¶æ³ã§ä½¿ãåããããã°ãã³ã¼ãã¯ããåããããããªãã
ã©ããªç¶æ³ã§ã each ã§æ¸ããã¨ã¯ã§ãããã ãããeach ã ã使ãããªããã°ããã¨ããèãæ¹ã¯ããç¹ã§æ£ããããã®ãããªèãæ¹ã®äººã«ã¨ã£ã¦ã¯each ã§æ¸ãã®ããã£ã¨ãåãããããã³ã¼ãã«ãªãã ããã
ãããæ £ãã¦ã¿ãã¨ä¸è¨ã®ã¡ã½ããã使ãåããããæ¹ãç°¡æ½ã§åãããããã³ã¼ãã«ãªãããã®çç±ã¯ã¡ãã»ã¼ã¸æ§ã®éãã ã
each ã§ãã¹ã¦ãæ¸ãå ´åã¯ä½è¨ãªã³ã¼ããæ¸ãå¿ è¦ãããããã®åãã©ããã¦ããæ¬è³ªçãªã³ã¼ããåããã¦ãã¾ããä½åãªã³ã¼ãããªãã»ã©ãæ¬è³ªçãªã³ã¼ããéç«ã¤ãã¡ãã»ã¼ã¸ãä¼ããããããªããæå³ãããã³ã¼ãã®æ¯çãä¸ãããã¨ã¯ãä¸ç´ããã°ã©ãã¸ã®éãéãéµã ã
ããã§ã¯æ¬é¡ã«å ¥ããã
ã¾ãã¯ç°¡åã« [1, 2, 3] ãããããäºä¹ã㦠[1, 4, 9] ãå¾ãã³ã¼ããæ¸ãã
ãªããeach_with_object ã®å©ç¨ã«ã¯ Ruby 1.9 ç³»ãå©ç¨ããããRails ã®ä¸é¨ã§ãã ActiveSupport ãå©ç¨ããå¿ è¦ãããã
# each
result =
[1, 2, 3].each do |i|
result << i*i
end
result # å¤ãè¿ãããã«å¿
è¦
# each_with_object
[1, 2, 3].each_with_object do |i, result|
result << i*i
end
# inject
[1, 2, 3].inject [] do |result, i|
result << i*i
end
# map
[1, 2, 3].map do |i|
i*i
end
ãã®å ´å㯠map ããã£ã¨ãã³ã¼ããçãç°¡æ½ã«ãªããã¨ãåãããeach ã®å ´åã¯è¿ãå¤ã self ã§ãããããçæãã result ãæ示çã«è¿ãå¿ è¦ãããã
ãã®å ´åã¯ã each_with_object 㨠inject ã®éã®éãã¯å¼æ°ã®é åºä»¥å¤ã«ç¹ã«åãããªãã
ã§ã¯ã次ã®ä¾ãè¦ã¦ã¿ããã
[%w(0xff0000 red), %w(0x00ff00 green), %w(0x0000ff blue)] ãã {"0xff0000" => "red", "0x00ff00" => "green", "0x0000ff" => "blue"} ãå¾ããã
# each hash = {} [%w(0xff0000 red), %w(0x00ff00 green), %w(0x0000ff blue)].each do |key, value| hash[key] = value end hash # each_with_object [%w(0xff0000 red), %w(0x00ff00 green), %w(0x0000ff blue)].each_with_object({}) do |(key, value), hash| hash[key] = value end # inject [%w(0xff0000 red), %w(0x00ff00 green), %w(0x0000ff blue)].inject({}) do |hash, (key, value)| hash[key] = value hash # <- ä½åã ãå¿ è¦ãªï¼è¡ end # map # ããã·ã¥ãè¿ãå¤ã¨ã§ããªãããããããªãã
each_with_object ã§ã¯å¿ ãå¼æ°ã®ãªãã¸ã§ã¯ããè¿ãå¤ã¨ãªããããè¿ãå¤ã¨ãªããªãã¸ã§ã¯ããæ示çã«æå¾ã«æ¸ãå¿ è¦ã¯ãªããããããªããã inject ã§ã¯ãããã¯ã§æå¾ã«è©ä¾¡ããå¤ã次åã®ãããã¯ã®å¼æ°ã¨ãªãããã«ã¯è¿ãå¤ã¨ãªããããæ示çã« hash ãæå¾ã«æ¸ãå¿ è¦ãããã
map ã¯é åãè¿ãå¤ã¨ããå ´åã«ããå©ç¨ã§ããªããããããã·ã¥ãå¾ããå ´åã¯å©ç¨ã§ããªãã
ãã®å ´åã¯ãeach_with_object ã使ãä¾ããã£ã¨ãç°¡æ½ã§ãããããã¯ãå é¨ã®å¦çã§å¯ä½ç¨ãããããå¦çãè¡ã£ã¦ããããã®å¯ä½ç¨ãä¸ããã対象ãè¿ãããå¤ã ããã ãinject ã®å¦çã§ã¯é©åãªå¤ãè¿ãããã« ï¼è¡ä½åã«å¿ è¦ã«ãªã£ã¦ãã¾ãã
次ã«æ´æ°ã®åãæ±ããä¾ã示ãã
# each sum = 0 [ 1, 2, 3].each do |i| sum += i end sum # each_with_object # ç ´å£çã«å¤æ´å¯è½ãªãªãã¸ã§ã¯ãã§ãªãã¨é©ç¨ä¸å¯ sum = [ 1, 2, 3].each_with_object(0) do |i, j| j += i end # p sum #=> 0 # inject ãã®ï¼ sum = [ 1, 2, 3].inject 0 do |subtotal, i| subtotal + i end # inject ãã®ï¼ sum = [ 1, 2, 3].inject 0, :+ # inject ãã®ï¼ sum = [ 1, 2, 3].inject :+ # map ã¯é åãè¿ãå¤ã¨ããå ´åã«ããå©ç¨ä¸å¯
æ´æ°ã®åãå¾ããå ´åã¯ãinject ãä¸çªç°¡æ½ã«ãªãããã®å ´å㯠each_with_object ã¯æå³ã©ããåä½ããªãã
æå¾ã«ãæååã®é£çµå¦çãèããã
# each path = "" %w(usr local ruby ruby1.9.1 bin).each do |dir| path << "/" + dir end path # each_with_object %w(usr local ruby ruby1.9.1 bin).each_with_object "" do |dir, path| path << "/" + dir # path += "/" + dir ã¯ä¸å¯ end # inject %w(usr local ruby ruby1.9.1 bin).inject "" do |path, dir| path + "/" + dir end # map # ä»åã®ä¸»ãªè¶£æ¨ã¨ã¯ç°ãªãã Array#join ã使ã解ã示ãã %w(usr local ruby ruby1.9.1 bin).map do |dir| "/" + dir end.join("")
ãã®å ´åã inject ãç°¡æ½ã§ããããã«æããããããããã¯ä¸»ã«each_with_object ãã¹ãã«ãé·ãããã ã
ãã®é¨åãå·®ãå¼ãã¦èããã°ãåçã ãããeach_with_object ã¯ãããã¯å¼æ°ã®ãªãã¸ã§ã¯ããç ´å£çã«å¤æ´ãç¶ããå ´åã«ä¾¿å©ãªã¡ã½ãããªã®ã§ããã®å ´å path += ã§ã¯ãªã path << ãå©ç¨ããå¿ è¦ããããinject ã¯ãããã¯ã®æå¾ã®è©ä¾¡çµæãå©ç¨ãããããpath << ã path += ãªã©ã¨ããå¿ è¦ã¯ãªããåã« + ã¨ãã¦è¯ãã
map 㨠join ãçµã¿åãããä¾ããæ¡å¤ç°¡æ½ã§åãããããç¹ãè¦è½ã¨ãã¹ãã§ãªãããã®ä¾ãããããã
ç·ã㦠each ã使ãã¨ãè¿ããããªãã¸ã§ã¯ãã®åæåã¨ãå¤ãè¿ãããã«ï¼è¡é·ããªãããã®ï¼è¡ã¯æ¬è³ªçãªå¦çãè¡ã£ã¦ããç®æã§ã¯ãªããããeach_with_object ã injectãmap ãå©ç¨ããã°ï¼è¡çãã§ããã
éã«ããã¨ããã§ã«å¾ããã¦ãããªãã¸ã§ã¯ãã«å¯¾ããå¦çã§ãè¿ãå¤ãªã©ãå©ç¨ããå¿
è¦ãã¨ãã«ãªãå ´å㯠each ãå©ç¨ããæ¹ãåãããããã³ã¼ãã«ãªã
è£è¶³ã§ããããtap (Ruby 1.8.7 以éï¼ã¨ããã¡ã½ããããreturning ï¼ActiveSupport ãæä¾ï¼ã¨ããã¡ã½ããããããããããå©ç¨ããã¨ãè¿ãå¤ã®ï¼è¡ãªã©ããªãããå ´åãããã
# tap {}.tap do |hash| [%w(0xff0000 red), %w(0x00ff00 green), %w(0x0000ff blue)].each do |key, value| hash[key] = value end end # returning returning ({}) do |hash| [%w(0xff0000 red), %w(0x00ff00 green), %w(0x0000ff blue)].each do |key, value| hash[key] = value end end
è足ã ããããã«ä¸è¨ã¯ Hash[] ã¡ã½ããã使ããã¨ã§ãããã¦ç°¡æ½ã«æ¸ããã
a = [%w(0xff0000 red), %w(0x00ff00 green), %w(0x0000ff blue)] Hash[*a.flatten]