0

I'm using Axlsx gem for excel sheet creation.

sheet name position going wrong after apply Ruby Thread.

for example:

i want sheet order in this way

sheet 1
sheet 2
sheet 3

but after apply ruby thread its coming in

sheet 2
sheet 1
sheet 3

how to keep sheet in order ?

Akash Kumar
  • 254
  • 2
  • 5
  • 20

1 Answers1

0

This will work based on your RubyFiddle example

arr = (1..10).map do |i|
  Thread.new{
     sleep(rand(0)/10.0)
     Thread.current["mycount"] = count
     count += 1
  }
end
arr.each do |t| 
  t.join
  puts "#{t['mycount']}"
end
    #24
    #28
    #29
    #26
    #23
    #22
    #25
    #21
    #27
    #20

arr.sort_by{|t| t["mycount"]}.each do |t| 
  t.join
  puts "#{t['mycount']}"
end
   #20
   #21
   #22
   #23
   #24
   #25
   #26
   #27
   #28
   #29

Or nicer still although probably does not work for your full solution of exporting spreadsheets

arr.sort_by(&:value).each(&:join)
engineersmnky
  • 25,495
  • 2
  • 36
  • 52