This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Changing the Way an Object Iterates
CodeExpectedActual
array = %w{bob loves alice}
array.collect { |x| x.capitalize }
["Bob", "Loves", "Alice"] ["Bob", "Loves", "Alice"]
array.collect_reverse { |x| x.capitalize }
["Alice", "Loves", "Bob"]
Error! (Exception?) Here's stdout:
NoMethodError: undefined method `collect_reverse' for ["bob", "loves", "alice"]:Array
	from (irb):3
require 'enumerator'
reversed_array = array.to_enum(:reverse_each)
reversed_array.collect { |x| x.capitalize }
["Alice", "Loves", "Bob"] ["Alice", "Loves", "Bob"]
reversed_array.each_with_index do |x, i|
  puts %{#{i}=>"#{x}"}
end
0=>"alice"
1=>"loves"
2=>"bob"
0=>"alice"
1=>"loves"
2=>"bob"
reversed_array[0]
NoMethodError: undefined method `[]' for #<Enumerable::Enumerator:0xb7c2cc8c>
Error! (Exception?) Here's stdout:
NoMethodError: undefined method `[]' for #<Enumerable::Enumerator:0xb7c2c854>
	from (irb):10
array_with_index = array.enum_with_index
array_with_index.each do |x, i|
  puts %{#{i}=>"#{x}"}
end
0=>"bob"
1=>"loves"
2=>"alice"
0=>"bob"
1=>"loves"
2=>"alice"
array_with_index.each_with_index do |x, i|
  puts %{#{i}=>#{x.inspect}}
end
0=>["bob", 0]
1=>["loves", 1]
2=>["alice", 2]
0=>["bob", 0]
1=>["loves", 1]
2=>["alice", 2]
sentence = %w{Well, now I've seen everything!}
two_word_window = sentence.to_enum(:each_cons, 2)
two_word_window.each { |x| puts x.inspect }
["Well,", "now"]
["now", "I've"]
["I've", "seen"]
["seen", "everything!"]
["Well,", "now"]
["now", "I've"]
["I've", "seen"]
["seen", "everything!"]
two_words_at_a_time = sentence.to_enum(:each_slice, 2)
two_words_at_a_time.each { |x| puts x.inspect }
["Well,", "now"]
["I've", "seen"]
["everything!"]
["Well,", "now"]
["I've", "seen"]
["everything!"]