committer: [email protected] • (Sorted)Setͷ࡞ऀ • Enumeratorͷ࡞ऀ • Rails: 4.1ʙ (2014) • Team Nokogiri, Team Huginn, … • A Father Of Two !" • Working at Machimachi Inc.
["A", "B", "D", "C"] array1.each_with_index do |e1, i| e2 = array2[i] # This is OK end ͨͩɺఴ͑ࣈΞΫηεͰ͖ͳ͍ΠςϨʔλಉ࢜ͷ߹͍ͬͨΜྻԽ͢ΔͳͲͷલॲཧ͕ඞཁɻ ·ͨɺঢ়گʹΑͬͯยํ͚ͩϙΠϯλΛਐΊΔɺͷΑ͏ͳॲཧΦϑηοτͷཧඞཁʹͳ Δɻ
(Jun 2008) ͕ͯདྷΔ1.9.1ͱͷΪϟοϓΛݮΒ͔ͨͬͨ͠ • 1.9.1Ͱͷ҆ఆ൛ϦϦʔεΛ߇͑ɺ͞Βʹ͔͜͜ΒEnumeratorͷ༷٧Ί͕ਐΉ… • ϝϞΦϒδΣΫτͷՃػೳ (Iteration with a memo object) • ΠϯσοΫεͷՃ (Iteration with an index number)
by candlerb ʮ͜ΕͰҰԠͰ͖ΔΑʯ Enumerator.new { |y| collection1.each { |x| y << x } collection2.each { |x| y << x } }.each do |x| # Enumerates collection1, and then collection2 end
"bar", "baz"] [nil].chain(a).each_cons(2) do |prev, curr| if prev.nil? puts "#{curr} comes first." else puts "#{curr} comes after #{prev}." end end # Output: foo comes first. bar comes after foo. baz comes after bar.
3].lazy # Expecting #each would create a normal Enumerator... still_lazy = lazy.each # Not: Enumerator::Lazy#each is overridden to return a Lazy # Not usable as a normal Enumerator p still_lazy.map { |x| x * 2 } #=> #<Enumerator::Lazy: #<Enumerator::Lazy: [1, 2, 3]>:map> • lazyΛۦͯ͠࡞ͬͨEnumeratorΛɺmap͢Δͱྻ͕ฦΔΑ͏ͳ;ͭ͏ͷEnumerableΦϒδΣΫτ ͱͯ͠ฦ͍ͨ͠