From decaea0f5f0619ca3de31a3a04c05b0134842035 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 6 Feb 2010 10:12:57 -0500 Subject: [PATCH] adding 'by' to array comprehensions --- lib/coffee_script/nodes.rb | 5 +++-- test/fixtures/execution/test_array_comprehension.coffee | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 87648c24..6b0ce78d 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -848,8 +848,9 @@ module CoffeeScript for_part = "#{index_var}=0, #{source.compile(o.merge(:index => ivar, :step => @step))}, #{index_var}++" else index_var = nil - source_part = "#{svar} = #{source.compile(o)};\n#{idt}" - for_part = @object ? "#{ivar} in #{svar}" : "#{ivar} = 0; #{ivar} < #{svar}.length; #{ivar}++" + source_part = "#{svar} = #{@source.compile(o)};\n#{idt}" + step_part = @step ? "#{ivar} += #{@step.compile(o)}" : "#{ivar}++" + for_part = @object ? "#{ivar} in #{svar}" : "#{ivar} = 0; #{ivar} < #{svar}.length; #{step_part}" var_part = "#{body_dent}#{@name} = #{svar}[#{ivar}];\n" if @name # body.unshift(AssignNode.new(@name, ValueNode.new(svar, [IndexNode.new(ivar)]))) if @name end diff --git a/test/fixtures/execution/test_array_comprehension.coffee b/test/fixtures/execution/test_array_comprehension.coffee index 03c1e1f7..08350a93 100644 --- a/test/fixtures/execution/test_array_comprehension.coffee +++ b/test/fixtures/execution/test_array_comprehension.coffee @@ -40,3 +40,9 @@ for method in methods print obj.one() is "I'm one" print obj.two() is "I'm two" print obj.three() is "I'm three" + + +# Steps should work for array comprehensions. + +array: [0..10] +print num % 2 is 0 for num in array by 2