on master help sign in
More idiomatic code for the ruby fold benchmark
06a5572Jasper Van der Jeugt
tests/benchmarks/ruby/fold.rb+4−4
@@ -10,7 +10,7 @@ def fold(filename, max_width)
10 file.each_line do |line|
10 file.each_line do |line|
11 # If we encounter an empty line, we reformat and dump the current
11 # If we encounter an empty line, we reformat and dump the current
12 # paragraph
12 # paragraph
13− if line.strip.empty? then
13+ if line.strip.empty?
14 puts fold_paragraph(paragraph, max_width)
14 puts fold_paragraph(paragraph, max_width)
15 puts
15 puts
16 paragraph = []
16 paragraph = []
@@ -32,11 +32,11 @@ def fold_paragraph(paragraph, max_width)
32 width = str.length
32 width = str.length
33
33
34 paragraph.drop(1).each do |word|
34 paragraph.drop(1).each do |word|
35− if width + word.length + 1 <= max_width then
35+ if width + word.length + 1 <= max_width
36− str += ' ' + word
36+ str << ' ' + word
37 width += word.length + 1
37 width += word.length + 1
38 else
38 else
39− str += "\n" + word
39+ str << "\n" + word
40 width = word.length
40 width = word.length
41 end
41 end
42 end
42 end