Monday, 15 February 2010

ruby - Range loop works by itself, but not inside an until loop -



ruby - Range loop works by itself, but not inside an until loop -

def bubble_sort(arr) sorted = false until sorted sorted = true (0...arr.length-2).each |i| if arr[i] > arr[i+1] arr[i], arr[i+1] = arr[i+1], arr[i] sorted = false end end end arr end bubble_sort([4,6,2,8,1])

the next error message:

(eval):367: (eval):367: compile error (syntaxerror) (eval):359: syntax error, unexpected kdo_cond, expecting kend (0...arr.length-2).each |i| ^ (eval):367: syntax error, unexpected kend, expecting $end

i don't understand why saying unexpected kend

this appears bug in ruby 1.8.7 - replacing do/end {}:

(0...arr.length-2).each |i| if arr[i] > arr[i+1] arr[i], arr[i+1] = arr[i+1], arr[i] sorted = false end end

and

(0...arr.length-2).each { |i| if arr[i] > arr[i+1] arr[i], arr[i+1] = arr[i+1], arr[i] sorted = false end }

fixes it.

ruby

No comments:

Post a Comment