1

I am trying to call a C program from my Ruby script, parsing it an argument (file object) and then store some variables the C program would return.

The idea is that my Ruby script allows me to easily cycle through the files & folders of a parent folder but it is way too slow to efficiently process all the files in that folder. Hence the use of a C program that I want to call to process each file.

My problem is that I can't find a method to call that C program from Ruby (and how to parse it the file argument, I'm not even sure it is possible as I don't know if Ruby files objects and C streams are "compatible")

Thank you in advance for your help !

3

2 Answers 2

2

You say you are trying to call a program so I assume you are not trying to statically or dynamically load a library and call a function. (If you are trying to load a library to call a function then look to the DL::Importer module.)

As for calling an external program from Ruby and receiving its result (from stdout, in this case), regardless of whether it was written in C or not, an easy way to do it is:

value = `program arg1 arg2 ...`

e.g. if the program you want to call compresses a given file and outputs the compressed size.

size = `mycompressionprogram filename.txt`
puts "compressed result is: #{size}"

Note those are back ticks " ` ".

So this is one easy way to code your computationally heavy stuff in C and wrap it up in a Ruby script.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you very much for your help, this is indeed really simple (feeling dumb not to have found it now :D)
I tried your solution as well with C:/Users/.../Test(oddly the barticks don't want to show in the code field) but got the same error than with fennec's solution : "No such file or directory"... Could you help me please ?
Okay, one thing to check: Whatever string you are putting into the backticks to be executed, if you paste it onto the command line and run it manually, does it work exactly as expected? e.g. is the executable file in your PATH? e.g. is the file actually executable? e.g. are there spaces in the path or string that could be screwing things up
If you supply the full path to the executable (the binary) then it should run, whether from the command line or from within the backticks as suggested in my solution. e.g. /home/user1/Project/bin/Debug/program arg1 ...
Ok finally my problem was that my compiler was building my program in a directory it shouldn't so I was calling the wrong executable. Thanks for your help
|
1

One simple traditional way for a Ruby process to interact with unrelated C code is popen, which will allow your Ruby process to invoke the (compiled) code as a separate process, passing your choice of arguments into the traditional space the operating system allocates for that (accessible in argv in your process's int main(int argc, char** argv)), and then interacting with its standard input and standard output over a pipe. However, this technique launches another process and requires that you serialize/deserialize any ongoing interprocess communication so that it can run over the pipe, which may be an impediment.

So you can also write the C code as a Ruby extension, which will allow you to return values more readily, and moreover avoids the overhead associated with having a separate process involved. However, note that if you perform extensive work with Ruby objects in your C code you may still incur the performance penalties you'd hoped to avoid. The canonical document on how to write Ruby extensions is README.EXT.

4 Comments

Thank you so much, it looks a bit complex at first so I'll have to study that a bit before using it ^^
I tried your solution with IO.popen('C:/Users/.../Test') and Ruby threw me an error "No such file or directory"... Could you help me find out why please ?
@NicolasSounac - If you see an error about "no such file or directory" you should expect that the path you passed it is not the path to a file or directory, let alone an executable file. Perhaps you're missing a file extension like Test.exe.
Thanks for your help, this was indeed a problem with the path I was parsing as argument. Though now the program still won't run and will juste stop working without sending any error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.