0

How do I execute Ruby files from another Ruby file?

class Launch
  def get_program
    begin
      files = ["sum_of_digits", "compressed_sequence", 
              "shortest_repetition"]
      (0...files.length).each_with_index do |index|
        puts "#{index} . #{ files[index]}"
      end  
      begin
        puts "Enter program number to execute: "
        puts program_number = gets.chomp.to_i   
        puts "loading program #{files[program_number]}"
        begin
          load(`ruby #{files[program_number]}.rb 
              #{files[program_number]}.txt`)
        rescue
          puts "loading error"
          end
        puts "do you want to continue Y/N"
        answer = gets.chomp
      end until answer == 'N'                       
    rescue
      puts "the file cannot be loaded ,it may be moved or not exist "
    end
  end
end

launch = Launch.new
launch.get_program

launch = Launch.new
launch.get_program

While executing, I am getting the output, but for only one program, and the loop is terminating. I want to execute files in a loop until the user enters "N".

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
gowthami
  • 155
  • 1
  • 10

1 Answers1

0

In general your code isn't written in the Ruby way. This is untested but it looks about right:

class Launch

  FILES = ['sum_of_digits', 'compressed_sequence', 'shortest_repetition']

  def get_program
    FILES.each_with_index do |fname, i|
      puts "#{i} . #{fname}"
    end  

    loop do
      puts "Enter program number to execute: "
      program_number = gets.to_i   
      file_to_load = FILES[program_number]
      puts "loading program #{file_to_load}"

      begin
        system("ruby #{file_to_load}.rb #{file_to_load}.txt")
      rescue => e
        puts "loading error: #{e}"
        puts "'#{file_to_load}' cannot be loaded, it may have been moved or not exist."
      end

      puts 'Do you want to continue Y/N'
      break if gets.chomp.strip.upcase == 'N'
    end
  end
end

launch = Launch.new
launch.get_program

Some things to study:

  • block and end are used to start exception handling, not to define control loops. Well, they can, but there are better, more idiomatic, ways. loop is recommended by Matz.
  • You used load but I don't think that's really what you'd want to do. Instead, you should tell the OS to load and run the code in a sub-shell using system, not in the context of your currently running code.
  • Instead of using a bare rescue, your code should at least capture the exception using rescue => e so you can output what occurred. In "real life", AKA, production, you should be even more discerning and capture only the exceptions you expect, but that's a different discussion.
  • When using a begin/rescue/end, try to keep them as small as possible, at least until you're more familiar with how they work. rescue is a great way to shoot yourself in the foot, and debugging raised exceptions that could be generated by many lines of code can be a pain.

In general, when you have a list of things that's likely to change, or any variable that's more likely to change than the rest of the code, put that definition at the top of the script, or the top of the class or module definition, then reference it as a constant. That helps avoid magical dust being sprinkled through the code that has to be searched for if you want to add or delete things. Like files. Or magical dust.

Community
  • 1
  • 1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303