1 require File.dirname(__FILE__) + '/../spec_helper'
3 # for name[, name]... in expr [do]
6 describe "The for expression" do
7 it "iterates over an Enumerable passing each element to the block" do
15 it "iterates over an Hash passing each key-value pair to the block" do
19 for i, j in { 1 => 10, 2 => 20 }
28 it "iterates over any object responding to 'each'" do
31 (0..10).each { |i| yield i }
42 it "allows an instance variable as an iterator name" do
52 # TODO: commented out due to a compiler error
53 #it "allows a class variable as an iterator name" do
63 it "splats multiple arguments together if there are fewer arguments than values" do
66 [[1,2,3], [4,5,6]].each do |a|
76 qs.should == [[1,2,3], [4,5,6]]
80 it "optionally takes a 'do' after the expression" do
88 it "allows body begin on the same line if do is used" do
90 for i in 1..3 do j += i
95 it "executes code in containing variable scope" do
103 it "executes code in containing variable scope with 'do'" do
112 for i in 1..3; end.should == (1..3)
113 for i,j in { 1 => 10, 2 => 20 }; end.should == { 1 => 10, 2 => 20 }
116 it "breaks out of a loop upon 'break', returning nil" do
127 it "allows 'break' to have an argument which becomes the value of the for expression" do
133 it "starts the next iteration with 'next'" do
144 it "repeats current iteration with 'redo'" do
149 redo if i == 2 && j < 4
155 it "repeats the loop from the beginning with 'retry'" do
160 retry if i == 3 && j < 7