-1

How do i run/execute a ruby script inside a c or c++ program? instead of typing irb in the Terminal and then ruby rubyfile.rb to run it.

Normally when i want to run/execute a makefile with command "flash" i would do:

#include <stdlib.h>

system("make flash");

I'm using mac by the way

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • 2
    Does [this question](http://stackoverflow.com/questions/239315/how-to-embed-ruby-in-c) help you? Or maybe [this one](http://stackoverflow.com/questions/626333/embedding-a-ruby-interpreter-in-a-c-app)? – Björn Pollex Dec 10 '12 at 07:57
  • Not really what i'm looking for – Jhsajda Hhas Dec 10 '12 at 07:59
  • 2
    system("ruby xxxx.rb"); does it work? – ray_linn Dec 10 '12 at 07:59
  • What about typing "irb" to start with or it not needed in that case? – Jhsajda Hhas Dec 10 '12 at 08:00
  • 1
    @JhsajdaHhas: Have you tried anything? Can you explain more clearly what you are trying to do? Why do you want to invoke the interactive shell? – Björn Pollex Dec 10 '12 at 08:03
  • Do you actually know what `irb` does? You don’t type that before executing a ruby script. – Konrad Rudolph Dec 10 '12 at 08:04
  • Yes i have tried to say: system("ruby file.rb");, but it says: ruby: No such file or directory -- file.rb (LoadError) – Jhsajda Hhas Dec 10 '12 at 08:08
  • Also tried, char cmd[1040]; char cwd[1024]; if (getcwd(cwd, sizeof(cwd)) != NULL) { snprintf(cmd, sizeof(cmd), "ruby -C \"%s\" file.rb", cwd); system(cmd); } – Jhsajda Hhas Dec 10 '12 at 08:11
  • 1
    Jhasajda, type `system("pwd")`. Your program's working directory is probably not the same as where your ruby file is contained. Consider using the absolute path rather than the relative path. Also, you don't need `irb` to run a ruby program. You can just type `ruby file.rb` in the terminal (assuming `file.rb` is contained in your working directory). – sunnyrjuneja Dec 10 '12 at 08:53
  • Why do you write `#include ` in your code snippet ? A C directive in a Ruby program ? Do you really want to run embedded Ruby from C++. I could document it, but I need to be sure it's what you want. – BernardK Dec 10 '12 at 13:17

1 Answers1

1

you can use a function of exec function group.

#include <unistd.h>
exec..()  #exec function group

you can type

man 3 exec

in terminal,to help you.