简体   繁体   中英

Ruby skips a section of script

Ruby noob here, and I am having an infuriating issue.

Something in my script is making Ruby skip a section of it.

#Main_block_begins_here
if __FILE__ == $0
  #Open_the_file_with_values_and_weights                    
  File.open(getFileName, 'r') do |f|
    #Intial_data...
    $totalNumber=f.gets.chomp!.to_i
    $totalCapacity=f.gets.chomp!.to_i
    $currentItemWeight=0
    $currentItemValue=0
    $spaceLeft=0
    $spaceLeft=$totalCapacity
    $takenWeight=0
    $takenValue=0

    #Make_a_heap_for_the_data
    $prioQ = Heap.new do
      #reads_values,_computes_them,_and_enqueues_them                    
      until i==$totalNumber
        total=0
        value=f.gets.chomp!.to_i
        weight=f.gets.chomp!.to_i
        total=value/weight
        puts "#{total}=t, #{value}=v, #{weight}=w. Correct?"
        check=gets.chomp!
        it=Obj.new(total, value, weight) do
          prioQ.enqueue(it)
        end
        i+=1
      end 
    end

    #dequeueIt

    puts "total weight taken was #{$takenWeight} and total value taken was #{$takenValue}."
  end
end

The commented line #dequeueIt is a method earlier on that when I let run, it gives me an infinite loop with all values that were supposed to be read in from the text file as zeros.

The puts line and the check declaration line inside the until loop are for debugging purposes, and of course they never print out.

Commented out, when I run the program it just prints out the last line as if the until loop never ran. If more code for context is necessary, just let me know and I'll put it up.

My hair's starting to fall out over this one, so any help is appreciated!

EDIT:: Yes, it should have been i==$totalnumber. I fixed it in my code and it still doesn't execute the script inside that loop.

This is one oversight:

until i=$totalNumber

This assigns a number to i; it is always true. Try

until i==$totalNumber

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM