Skip to content

Instantly share code, notes, and snippets.

@MSch
Created June 3, 2012 17:55
Show Gist options
  • Save MSch/2864359 to your computer and use it in GitHub Desktop.
Save MSch/2864359 to your computer and use it in GitHub Desktop.
Ruby 1.9 enumerator chaining
[1,2,3].map.with_index { |x, i| puts "#{x} #{i}"; x+1 }
# 1 0
# 2 1
# 3 2
# => [2, 3, 4]
[1,2,3].each.with_object("q") { |x, obj| puts "#{x} #{obj}"; obj << x.to_s }
# 1 q
# 2 q1
# 3 q12
# => "q123"
[1,2,3].each.with_index.with_object("q") { |(x, i), obj| puts "#{x} #{i} #{obj}"; obj << "/#{x}@#{i}" }
# 1 0 q
# 2 1 q/1@0
# 3 2 q/1@0/2@1
# => "q/1@0/2@1/3@2"
[1,2,3].each.with_object("q").with_index { |(x, obj), i| puts "#{x} #{i} #{obj}"; obj << "/#{x}@#{i}" }
# 1 0 q
# 2 1 q/1@0
# 3 2 q/1@0/2@1
# => "q/1@0/2@1/3@2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment