0

I have a compiled c++ file that I would like to run from within a temporary directory in ruby:

folder = Dir.mktmpdir
      begin
      puts folder
      File.open("#{folder}/input.xml", 'w') { |file| file.write(builder.to_xml) }

      ensure
      system '.././program'
      FileUtils.remove_entry folder
      end

The above code properly creates all the desired file/folder, but does not run the system call, and does not produce an error. I have tried the all of the varieties listed here: Calling shell commands from Ruby but cannot seem to get it to function. If I comment out the file deletion line and manually go and execute the command listed above, the program functions properly. Also, if I instead save the file to the same location as the ./program, the ./program works. What is preventing me from running the command from a sub-folder?

Community
  • 1
  • 1
user153509
  • 11
  • 1
  • 3
  • 1
    using `system` allows you to check it's return value, so if you did `puts system '.././program'` what does the console show? True = 0 exit status, False = non-zero exist status, nil = execution failed. Also you could try it in pry/irb without the need for puts. – Anthony Sep 16 '15 at 18:02
  • The console has a blank line representing where the output should be printed – user153509 Sep 16 '15 at 18:06
  • I don't think your path is right, I just tried using system from a variety of dir's and it's working. – Anthony Sep 16 '15 at 18:06
  • That means it was nil, the file wasn't found most likely – Anthony Sep 16 '15 at 18:06
  • 1
    The call to `system` does not run "from" any particular sub-folder, and I cannot see where you change directory? Are you expecting the command to be run with current directory equal to value of `folder`? You probably need to cd - see http://stackoverflow.com/questions/3339883/to-change-directory-inside-a-ruby-script - also consider using full paths, and alter your C++ program to accept a path to the file it is processing rather than loading in the file from the current dir. – Neil Slater Sep 16 '15 at 18:06
  • 1
    @NeilSlater: Or change the C++ program to accept the XML on the standard input and use [`Open3`](http://ruby-doc.org/stdlib-2.2.3/libdoc/open3/rdoc/index.html) to talk to it. – mu is too short Sep 16 '15 at 18:43
  • @NeilSlater, the problem was not being in the correct folder, and was resolved using Dir.chdir, Thank you very much – user153509 Sep 18 '15 at 18:42

1 Answers1

-1

maybe system finding a program has name .././program

try the backtick operator or %x function

puts `.././program`

puts %x{.././program}
Johnny Dương
  • 753
  • 3
  • 8