Put inline random numbers in Ruby

I have two counters with simple animations that put numbers under each other. How to make them all built-in with the same functionality? Thanks for watching!

1000.times do

    puts "Press Enter"
    gets

        x = rand (0..5)
        y = rand (0..5)

        # "x" counter
        10.times do |xx|

            meter = rand (0..5)
            if xx < 9
                print meter
                print "\r"
                sleep 0.08
                else print x
            end
        end

    puts

        # "y" counter
        10.times do |yy|

            meter = rand (0..5)
            if yy < 9
                print meter
                print "\r"
                sleep 0.08
                else print y
            end
        end

    puts

end
+4
source share
1 answer

Use the character "\ b" backspace (0x08) instead of the return character "\ r" (0x0d).

1000.times do

  puts "Press Enter"
  gets

  x = rand (0..5)
  y = rand (0..5)

  # "x" counter
  10.times do |xx|

    meter = rand (0..5)
    if xx < 9
      print meter
      print "\b"
      sleep 0.08
    else print x
    end
  end

  print ' '

  # "y" counter
  10.times do |yy|

    meter = rand (0..5)
    if yy < 9
      print meter
      print "\b"
      sleep 0.08
    else print y
    end
  end

  puts

end
+5
source

Source: https://habr.com/ru/post/1599438/


All Articles