jingyang81
jingyang81

Reputation: 473

What's the meaning of this Ruby code?

What operation is being done to the arr[i], arr[i+1] in the second line:

if arr[i] > arr[i + 1]
   arr[i], arr[i + 1] = arr[i + 1], arr[i]
   sorted = false
end

Upvotes: 1

Views: 70

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118261

What operation is being done to the arr[i], arr[i+1] in the second line.

arr[i], arr[i + 1] = arr[i + 1], arr[i] means value swapping to sort the array.

arr = [3,2]
i = 0
arr[i], arr[i + 1] = arr[i + 1], arr[i]
arr # => [2,3]

What's this type of ternary called?

This is called parallel assignment,not ternary.

Upvotes: 4

Related Questions