https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112009-06-06T01:26:45ZRuby Issue Tracking SystemRuby master - Bug #1573: $0 behaves unexpectedlyhttps://redmine.ruby-lang.org/issues/1573?journal_id=41922009-06-06T01:26:45Zrue (Eero Saynatkari)
<ul></ul><p>Excerpts from Luiz Angelo Daros de Luca's message of Fri Jun 05 19:07:18 +0300 2009:</p>
<blockquote>
<p>Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: $0 behaves unexpectedly (Closed)" href="https://redmine.ruby-lang.org/issues/1573">#1573</a>: $0 behaves unexpectedly<br>
<a href="http://redmine.ruby-lang.org/issues/show/1573" class="external">http://redmine.ruby-lang.org/issues/show/1573</a></p>
<p>Author: Morris Brodersen<br>
Status: Open, Priority: Normal<br>
Category: core<br>
ruby -v: ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-mswin32]</p>
<p>consider two files, foo.rb and bar.rb, both simply contain: "<code>p $0</code>". i run "<code>ruby -rbar.rb foo.rb</code>".</p>
<p>using 1.8.6 and the same setup, the output is:</p>
<pre><code>"foo.rb"
"foo.rb"
</code></pre>
<p>using 1.9.1p0 the output is:</p>
<pre><code>"ruby"
"foo.rb"
</code></pre>
<p>as a developer, i would expect the old (1.8.6) behavior. i've had arguments<br>
that the new behavior is more "correct", as there is no actual script running<br>
when the requiring of bar.rb takes place. however, from this correctness, there<br>
arises no advantage whatsoever. i am strongly in favor of the old behavior.</p>
</blockquote>
<p>I would argue that the 1.9 (and 1.8.7) version is a better<br>
representation of the actual state of the program.</p>
<p>Further, though, I cannot see a valid use case where the<br>
script being required with -r <em>should</em> know the name of<br>
the assumed eventual script: to me, it implies a problem<br>
with the design and/or the separation of responsibilities.</p>
<p>As always, there may be some use case I am missing?</p>
<p>The "<code>$0 == __FILE__</code>" trick obviously works the same in both<br>
implementations.</p>
<p>Regards,<br>
Eero</p>
<hr>
<p>Magic is insufficiently advanced technology.</p> Ruby master - Bug #1573: $0 behaves unexpectedlyhttps://redmine.ruby-lang.org/issues/1573?journal_id=41942009-06-06T03:06:53Zmorris (M B)morrisbrodersen@googlemail.com
<ul></ul><p>=begin</p>
<blockquote>
<p>As always, there may be some use case I am missing?</p>
</blockquote>
<p>my own use case is a bit hacky and has only syntax advantages:<br>
i'm trying to intercept certain command line arguments, for example<br>
in "ruby foo.rb -test" i would intercept the "-test" and<br>
exec "ruby ./.tests/t_foo.rb" (which is a test suite for foo.rb<br>
previously generated). this is all done in the script "pre.rb"<br>
which i require by the rubyopt environment var (-rpre.rb). it would<br>
need $0 to easily get name of the executed script; there are even<br>
hackier ways of course. i admit that this use case is probably not<br>
very meaningful nor a good design idea. i'd personally like it,<br>
though.</p>
<p>anyway, let us suppose there is no valid use case. you wrote:</p>
<blockquote>
<p>Further, though, I cannot see a valid use case where the<br>
script being required with -r <em>should</em> know the name of<br>
the assumed eventual script: to me, it implies a problem<br>
with the design and/or the separation of responsibilities.</p>
</blockquote>
<p>we can take this a step further to find ourselves posing<br>
the following questions:</p>
<ol>
<li>
<p>is there any difference between -r'd and require()'d scripts<br>
regarding the script that is actually run? no!</p>
</li>
<li>
<p>we can then ask: is their a valid use case for<br>
require()'d scripts to know $0?</p>
</li>
<li>
<p>if not, is their any valid use case except the<br>
$0 == <strong>FILE</strong> trick?</p>
</li>
<li>
<p>yet further, what is the point of having $0 if we only<br>
need it for the trick mentioned above?</p>
</li>
</ol>
<p>the main thing that buggers me though is that $0 == "ruby"<br>
comes totally unexpected. i would never expect it to hold<br>
"ruby" (except when the file itself is named ruby), and i<br>
don't think the average developer would.</p>
<p>i hope my points were clear enough.</p>
<p>regards, morris</p>
<p>update:<br>
additionally, why is ARGV fully available in the -r'd scripts?<br>
we'd have to artificially "hide" ARGV and $0 by your arguments.<br>
that does not make much sense to me.<br>
=end</p> Ruby master - Bug #1573: $0 behaves unexpectedlyhttps://redmine.ruby-lang.org/issues/1573?journal_id=100312010-04-10T11:34:01Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Assignee</strong> set to <i>nobu (Nobuyoshi Nakada)</i></li></ul><p>Hi,</p>
<blockquote>
<p>consider two files, foo.rb and bar.rb, both simply contain: "<code>p $0</code>". i run "<code>ruby -rbar.rb foo.rb</code>".</p>
<p>using 1.8.6 and the same setup, the output is:</p>
<pre><code>"foo.rb"
"foo.rb"
</code></pre>
<p>using 1.9.1p0 the output is:</p>
<pre><code>"ruby"
"foo.rb"
</code></pre>
</blockquote>
<p>I think this is not an intended change.</p>
<p>In fact, nobu fixed this at r23812 (accidentally?) once.<br>
But the symptom reoccers at r25330 for fixing another bug.</p>
<p>Nobu, how about the following patch?</p>
<pre><code class="diff syntaxhl" data-language="diff"><span class="gh">diff --git a/ruby.c b/ruby.c
index 5c1eef0..489942d 100644
</span><span class="gd">--- a/ruby.c
</span><span class="gi">+++ b/ruby.c
</span><span class="p">@@ -1353,6 +1353,8 @@</span> process_options(int argc, char **argv, struct cmdline_options *opt)
}
}
ruby_init_gems(!(opt->disable & DISABLE_BIT(gems)));
<span class="gi">+ rb_progname = opt->script_name;
+ rb_vm_set_progname(rb_progname);
</span> ruby_set_argv(argc, argv);
process_sflag(&opt->sflag);
<span class="err">
</span><span class="p">@@ -1397,8 +1399,6 @@</span> process_options(int argc, char **argv, struct cmdline_options *opt)
tree = load_file(parser, opt->script, 1, opt);
});
}
<span class="gd">- rb_progname = opt->script_name;
- rb_vm_set_progname(rb_progname);
</span> if (opt->dump & DUMP_BIT(yydebug)) return Qtrue;
<span class="err">
</span> if (opt->ext.enc.index >= 0) {
<span class="p">@@ -1590,7 +1590,12 @@</span> load_file_internal(VALUE arg)
else if (!NIL_P(c)) {
rb_io_ungetbyte(f, c);
}
<span class="gd">- require_libraries(&opt->req_list); /* Why here? unnatural */
</span><span class="gi">+ {
+ VALUE progname = rb_progname;
+ rb_progname = Qnil;
+ require_libraries(&opt->req_list); /* Why here? unnatural */
+ rb_progname = progname;
+ }
</span> }
if (opt->src.enc.index >= 0) {
enc = rb_enc_from_index(opt->src.enc.index);
</code></pre>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a></p> Ruby master - Bug #1573: $0 behaves unexpectedlyhttps://redmine.ruby-lang.org/issues/1573?journal_id=101452010-04-13T08:11:45Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>=begin<br>
Hi,</p>
<p>In message "Re: <a href="https://blade.ruby-lang.org/ruby-core/29403">[ruby-core:29403]</a> [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: $0 behaves unexpectedly (Closed)" href="https://redmine.ruby-lang.org/issues/1573">#1573</a>] $0 behaves unexpectedly"<br>
on Sat, 10 Apr 2010 11:34:01 +0900, Yusuke Endoh <a href="mailto:redmine@ruby-lang.org" class="email">redmine@ruby-lang.org</a> writes:</p>
<p>|Nobu, how about the following patch?</p>
<p>I am not him, but you go ahead.</p>
<pre><code> matz.
</code></pre>
<p>=end</p> Ruby master - Bug #1573: $0 behaves unexpectedlyhttps://redmine.ruby-lang.org/issues/1573?journal_id=114612010-06-04T16:24:55Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Priority</strong> changed from <i>Normal</i> to <i>3</i></li><li><strong>Target version</strong> set to <i>2.0.0</i></li></ul><p>=begin<br>
Hi,</p>
<blockquote>
<p>I am not him, but you go ahead.</p>
</blockquote>
<p>Sorry, my patch does not work, though I cannot remember what I thought<br>
when writing the patch...</p>
<p>$ ./ruby -I. -rbar foo.rb<br>
nil<br>
"foo.rb"</p>
<p>This issue is caused on r25330.<br>
The purpose of r25330 is to hide script name in load error of -r option:</p>
<p>before r25330:</p>
<p>$ ./ruby -rnotexist -ep<br>
-e:0:in `require': cannot load such file -- notexist (LoadError)</p>
<p>after r25330:</p>
<p>$ ./ruby -rnotexist -ep<br>
./ruby:0:in `require': cannot load such file -- notexist (LoadError)</p>
<p>But I prefer "-e:0". I suggest reverting r25330.</p>
<p>Anyway, it is too late to fix 1.9.2.<br>
This would be fixed after 1.9.3 or later.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a><br>
=end</p> Ruby master - Bug #1573: $0 behaves unexpectedlyhttps://redmine.ruby-lang.org/issues/1573?journal_id=114672010-06-05T09:57:05Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li><li><strong>% Done</strong> changed from <i>0</i> to <i>100</i></li></ul><p>=begin<br>
This issue was solved with changeset r28161.<br>
Morris, thank you for reporting this issue.<br>
Your contribution to Ruby is greatly appreciated.<br>
May Ruby be with you.</p>
<p>=end</p>