Forum: Ruby setting __FILE__ impossible with rb_eval_string() - hack ide

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ab030a5bfd0d7232b388d3a7449dc84e?d=identicon&s=25 John-Mason P. Shackelford (Guest)
on 2007-01-02 03:40
(Received via mailing list)
In the following C code:

int main(const int argc, const char ** argv){
  ruby_sourcefile = rb_source_filename(argv[0]);
  rb_eval_string("puts __FILE__");   /* --> returns (eval) wanted
argv[0] */
  rb_eval_string("puts $0");
  return 0;

my attempts to to set a value for __FILE__ fail because rb_eval_string
resets it to "(eval)" every time as shown below:

----- eval.c -----
01600 VALUE
01601 rb_eval_string(str)
01602     const char *str;
01603 {
01604     VALUE v;
01605     NODE *oldsrc = ruby_current_node;
01607     ruby_current_node = 0;
01608     ruby_sourcefile = rb_source_filename("(eval)");
01609     v = eval(ruby_top_self, rb_str_new2(str), Qnil, 0, 0);
01610     ruby_current_node = oldsrc;
01612     return v;
01613 }

Can anyone suggest a clever hack ( C novice here ) so that I can reset
__FILE__ to argv[0]? I thought that perhaps I could just call eval
directly but I can't seem to grab ruby_top_self from within the
context of my application, but I am a C ignoramus so I may be missing
something obvious.

I'd be grateful for any pointers (he, he).
John-Mason Shackelford

Software Developer
Pearson Educational Measurement

2510 North Dodge St.
Iowa City, IA 52245
ph. 319-354-9200x6214
This topic is locked and can not be replied to.