Ruby_script()

I am wondering what exactly ruby_script() is doing ?

Looking in README.EXT
void ruby_script(char *name)
Specifies the name of the script ($0).

I have tried both with&without ruby_script(), but I
don’t feel any difference :slight_smile:
ruby_init();
ruby_init_loadpath();
ruby_script(“embed”);

Why am I asking this question? Because I see ruby_script()
used everywhere… Im confused.

What is purpose with ruby_script() ?

···


Simon Strandgaard

Hi,

···

At Sun, 13 Apr 2003 12:33:05 +0900, Simon Strandgaard wrote:

I am wondering what exactly ruby_script() is doing ?

Changing $0 and source file name in stack trace.


Nobu Nakada

Are you telling me that I should be able to see the
souce filename during backtrace ?

Can you explain further ?

Thanks
Simon Strandgaard

···

On Sun, 13 Apr 2003 15:05:11 +0900, nobu.nokad wrote:

At Sun, 13 Apr 2003 12:33:05 +0900, > Simon Strandgaard wrote:

I am wondering what exactly ruby_script() is doing ?

Changing $0 and source file name in stack trace.

Can you explain further ?

pigeon% cat b.rb
#!/usr/bin/ruby
raise "out"
pigeon%

pigeon% b.rb
./b.rb:2: out (RuntimeError)
pigeon%

ruby display "./b.rb", at the beginning of the line, because it has called
ruby_script("./b.rb")

Guy Decoux

Can you explain further ?

pigeon% cat b.rb
#!/usr/bin/ruby
raise “out”
pigeon%

pigeon% b.rb
./b.rb:2: out (RuntimeError)
pigeon%

ruby display “./b.rb”, at the beginning of the line, because it has called
ruby_script(“./b.rb”)

Weird… I don’t see any “embed” in my backtraces?

./a.out -e ‘puts “hello”; raise “out”’
hello
-e:1: out (RuntimeError)
less main.cpp
#include <ruby.h>

int main(int argc, char *argv) {
ruby_init();
ruby_init_loadpath();
ruby_script(“embed”); // seems not to have any effect ?
ruby_options(argc, argv);
ruby_run();
}

When you are doing embedding are you then supposed to
call ruby_script() ?

···

On Sun, 13 Apr 2003 22:19:25 +0900, ts wrote:


Simon Strandgaard

./a.out -e 'puts "hello"; raise "out"'

You call -e it display -e

pigeon% cat a.c
#include <ruby.h>

static VALUE
tt()
{
    rb_raise(rb_eStandardError, "error");
    return Qnil;
}

int main(int argc, char *argv)
{
    int res;

    ruby_init();
    ruby_init_loadpath();
    ruby_script("embed"); // seems not to have any effect ?
    rb_protect(tt, 0, &res);
    if (res) {
        VALUE err = rb_inspect(ruby_errinfo);
        rb_backtrace();
        fprintf(stdout, "ERROR %s\n", StringValuePtr(err));
    }
    return 0;
}
pigeon%

pigeon% a.out
        from embed
ERROR #<StandardError: error>
pigeon%

Guy Decoux

Hi,

./a.out -e ‘puts “hello”; raise “out”’
hello
-e:1: out (RuntimeError)

Because the source is given by “-e”.

    ruby_script("embed");  // seems not to have any effect ?
    ruby_options(argc, argv);

ruby_options() calls ruby_script().

···

At Sun, 13 Apr 2003 21:53:49 +0900, Simon Strandgaard wrote:


Nobu Nakada

Hi,

./a.out -e ‘puts “hello”; raise “out”’
hello
-e:1: out (RuntimeError)

Because the source is given by “-e”.

    ruby_script("embed");  // seems not to have any effect ?
    ruby_options(argc, argv);

ruby_options() calls ruby_script().

OK… Now I’ve swapped them. But it makes no difference.

./a.out -e ‘puts “hello”; raise “out”’
hello
-e:1: out (RuntimeError)
less main.cpp
#include <ruby.h>

int main(int argc, char *argv) {
ruby_init();
ruby_init_loadpath();
ruby_options(argc, argv);
ruby_script(“embed”); // what is going on?
ruby_run();
}

···

On Sun, 13 Apr 2003 23:13:13 +0900, nobu.nokad wrote:

At Sun, 13 Apr 2003 21:53:49 +0900, > Simon Strandgaard wrote:


Simon Strandgaard

[snip good code example]

If you are doing a rb_require() then I see no “embed” message ?
Should’nt it say “embed” in the backtrace ??

./a.out
hello
from ./test.rb:2
ERROR #<RuntimeError: out>
cat main.cpp
#include <ruby.h>
#include
using namespace std;

VALUE myfunc(VALUE) {
rb_require(“test.rb”);
rb_raise(rb_eStandardError, “error”);
return Qnil;
}

int main(int argc, char *argv) {
ruby_init();
ruby_init_loadpath();
ruby_script(“embed”);
int error;
rb_protect(myfunc, 0, &error);
if(error) {
VALUE err = rb_inspect(ruby_errinfo);
rb_backtrace();
cout << "ERROR " << StringValuePtr(err) << endl;
}
return 0;
}

cat test.rb
puts “hello”
raise “out”

···

On Sun, 13 Apr 2003 23:12:39 +0900, ts wrote:


Simon Strandgaard

./a.out -e 'puts "hello"; raise "out"'

When ruby compile a script (or a string) it store in each node the name of
the file and the line number. These are these values that it display when it
print an error.

Because you have used -e the string 'puts "hello"; raise "out"' was
compiled with the name '-e' and when it display the error it use this
name.

For example

pigeon% cat b.rb
#!/usr/bin/ruby
raise "out"
pigeon%

pigeon% cat c.rb
#!/usr/bin/ruby
require 'b.rb'
pigeon%

pigeon% c.rb
./b.rb:2: out (RuntimeError)
        from ./c.rb:2:in `require'
        from ./c.rb:2
pigeon%

See the first line with './b.rb' and not './c.rb'

Guy Decoux

I think I got that… But what Im not understanding is
when you are doing rb_require() that “embed” is not written out?

Is’nt “embed” supposed to be outputted in the backtrace?

···

On Sun, 13 Apr 2003 23:46:40 +0900, ts wrote:

./a.out -e ‘puts “hello”; raise “out”’

When ruby compile a script (or a string) it store in each node the name of
the file and the line number. These are these values that it display when it
print an error.

Because you have used -e the string ‘puts “hello”; raise “out”’ was
compiled with the name ‘-e’ and when it display the error it use this
name.


Simon Strandgaard

Is'nt "embed" supposed to be outputted in the backtrace?

The backtrace are outputted for each frame created, when you write

pigeon% cat a.c
#include <ruby.h>

static VALUE
tt()
{
    rb_require("b.rb");
    return Qnil;
}

int main(int argc, char *argv)
{
    int res;

    ruby_init();
    ruby_init_loadpath();
    ruby_script("embed"); // seems not to have any effect ?
    rb_protect(tt, 0, &res);
    if (res) {
        VALUE err = rb_inspect(ruby_errinfo);
        fprintf(stdout, "ERROR %s\n", StringValuePtr(err));
        rb_backtrace();
    }
    return 0;
}
pigeon%

ruby create a frame *only* for rb_require() (rb_laod(), none of the
commands ruby_init(), ruby_init_loadpath(), rb_protect() create a frame.

Guy Decoux

Is’nt “embed” supposed to be outputted in the backtrace?

The backtrace are outputted for each frame created, when you write
[snip]
ruby create a frame only for rb_require() (rb_laod(), none of the
commands ruby_init(), ruby_init_loadpath(), rb_protect() create a frame.

I see… only rb_require(). BTW what is a frame?

Im writing a tutorial on embedding. The section on “initialization”.
What should I write: use ruby_script() it does something ?
http://metaeditor.sourceforge.net/embed/

I tried rb_load() … but I don’t see “embed” in the backtrace, why ?

./a.out
hello
from ./test.rb:2
ERROR #<RuntimeError: out>
cat main.cpp
#include <ruby.h>
#include
using namespace std;

VALUE myfunc(VALUE) {
//rb_require(“test.rb”, 0);
rb_load(rb_str_new2(“test.rb”), Qtrue); // no “embed” backtrace?
return Qnil;
}

int main(int argc, char *argv) {
ruby_init();
ruby_init_loadpath();
ruby_script(“embed”);
int error;
rb_protect(myfunc, 0, &error);
if(error) {
VALUE err = rb_inspect(ruby_errinfo);
rb_backtrace();
cout << "ERROR " << StringValuePtr(err) << endl;
}
return 0;
}

cat test.rb
puts “hello”
raise “out”

···

On Mon, 14 Apr 2003 00:25:49 +0900, ts wrote:

-------------------- Start SpamAssassin results ----------------------
This mail is probably spam. The original message has been altered
so you can recognise or block similar unwanted mail in future.
See Apache SpamAssassin: Welcome for more details.

I must take care : SpamAssassin think that you are a spammer :-)))

I see… only rb_require(). BTW what is a frame?

Well, it’s the internal of ruby : not important

I tried rb_load() … but I don’t see “embed” in the backtrace, why ?

rb_load() is what ruby call when you make a rb_require() on a script file
(not a dynamic library).

This is rb_load() which create the frame : because ruby has a frame it
display the backtrace only for these frames. Your program has never
created a frame (except for rb_load()) this is why ruby don’t display its
name

Guy Decoux

-------------------- Start SpamAssassin results ----------------------
This mail is probably spam. The original message has been altered
so you can recognise or block similar unwanted mail in future.
See Apache SpamAssassin: Welcome for more details.

I must take care : SpamAssassin think that you are a spammer :-)))

Exactly… I’ve converted to the dark side and im very dangerous!!

I see… only rb_require(). BTW what is a frame?

[snip ruby-internals]

Thanks (ts, nobu) for your efforts.
Im not that much confused any longer :slight_smile:

Still I don’t know what to write in that tutorial about initialization ???
Suggestions is welcome :slight_smile:

···

On Mon, 14 Apr 2003 01:45:58 +0900, ts wrote:


Simon Strandgaard