RosettaCodeData/Task/Dutch-national-flag-problem/Ruby/dutch-national-flag-problem.rb
2018-06-22 20:57:24 +00:00

25 lines
421 B
Ruby

class Ball
FLAG = {red: 1, white: 2, blue: 3}
def initialize
@color = FLAG.keys.sample
end
def color
@color
end
def <=>(other) # needed for sort, results in -1 for <, 0 for == and 1 for >.
FLAG[self.color] <=> FLAG[other.color]
end
def inspect
@color
end
end
balls = []
balls = Array.new(8){Ball.new} while balls == balls.sort
puts "Random: #{balls}"
puts "Sorted: #{balls.sort}"