https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112011-06-04T23:16:59ZRuby Issue Tracking SystemRuby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=174942011-06-04T23:16:59Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>I agree providing a method to tell whether it is loaded as a library or is executed as a command is more elegant than '<strong>FILE</strong> == $0', but I am still afraid #executed? can mean multiple ways so that the name can cause confusion. We need a better name.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=174952011-06-04T23:53:06ZCezary (Cezary Baginski)cezary.baginski@gmail.com
<ul></ul><p>On Sat, Jun 04, 2011 at 11:17:01PM +0900, Yukihiro Matsumoto wrote:</p>
<blockquote>
<p>I agree providing a method to tell whether it is loaded as a library<br>
or is executed as a command is more elegant than '<strong>FILE</strong> == $0',<br>
but I am still afraid #executed? can mean multiple ways so that the<br>
name can cause confusion. We need a better name.</p>
</blockquote>
<p>How about the inverse or something similar:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"> <span class="k">unless</span> <span class="n">required?</span> <span class="c1"># top-level file, not require'd or loaded</span>
<span class="c1">#main</span>
<span class="nb">puts</span> <span class="s2">"hello from main"</span>
<span class="k">end</span>
</code></pre>
<p>This would also protect from main being run twice when the file<br>
includes itself.</p>
<p>--<br>
Cezary Baginski</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=174962011-06-05T03:53:09Zsdsykes (Stephen Sykes)sdsykes@gmail.com
<ul></ul><p>Some other suggestions:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="n">main?</span>
<span class="k">if</span> <span class="n">first_script?</span>
<span class="k">if</span> <span class="n">main_script?</span>
<span class="k">if</span> <span class="n">run_script?</span>
</code></pre>
<p>-Stephen</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=175052011-06-05T15:01:09Zlazaridis.com (Lazaridis Ilias)ilias@lazaridis.com
<ul></ul><p>Some notes subjecting the naming:</p>
<p>The clarity of the method name should be rated in it's OO context, like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="nb">self</span><span class="p">.</span><span class="nf">executed?</span> <span class="c1"># or .started? .launched?, which means *not* .included? .required? .loaded?</span>
<span class="c1"># do main code</span>
<span class="k">end</span>
</code></pre>
<p>similarly, one could write:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">unless</span> <span class="nb">self</span><span class="p">.</span><span class="nf">executed?</span>
<span class="c1"># do inclusion code</span>
<span class="k">end</span>
</code></pre>
<p>"self" would be "the module" or "the file" or "the main object"</p>
<p>Possibly the most simple word would be "main?", but strictly from a OO view, this would be not correct.</p>
<p>Although, from a user view, it looks simple and recognizable:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="n">main?</span>
<span class="k">do</span> <span class="n">main</span> <span class="n">stuff</span>
<span class="k">end</span>
<span class="k">unless</span> <span class="n">main?</span>
<span class="k">do</span> <span class="n">inclusion</span> <span class="n">stuff</span>
<span class="k">end</span>
</code></pre>
<p>In any way, the name should be a compact one, e.g. without "<code>_</code>", like <code>is_started?</code></p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=175102011-06-05T16:29:07Zcjheath (Clifford Heath)clifford.heath@gmail.com
<ul></ul><p>On 05/06/2011, at 4:01 PM, Lazaridis Ilias wrote:</p>
<blockquote>
<p>The clarity of the method name should be rated in it's OO context,<br>
like this:</p>
</blockquote>
<p>I dispute the need for a method. That just forces someone to the<br>
documentation<br>
to know what the code means.</p>
<p>The problem with the existing solution "<code>if (__FILE__ == $0)</code>" is that<br>
the meaning is<br>
hidden. If the variables were named differently, this would be exactly<br>
the expression<br>
that would best communicate the intent. To exaggerate, what we need is:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="n">__THIS_FILE__</span> <span class="o">==</span> <span class="vg">$THIS_PROGRAM</span>
<span class="o">...</span>
<span class="k">end</span>
</code></pre>
<p>That is, the problem is that it's not obvious to a newcomer that <code>__FILE__</code> means<br>
the current source-code file, or that <code>$0</code> means the name of the script<br>
being<br>
executed. Especially the latter...</p>
<p>Clifford Heath.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=175172011-06-05T22:29:10ZEregon (Benoit Daloze)
<ul></ul><p>Clifford Heath wrote:</p>
<blockquote>
<p>That is, the problem is that it's not obvious to a newcomer that <strong>FILE</strong> means<br>
the current source-code file, or that $0 means the name of the script<br>
being executed. Especially the latter...</p>
</blockquote>
<p><code>$PROGRAM_NAME</code> is an alias for <code>$0</code>.<br>
But "<code>if __FILE__ == $PROGRAM_NAME</code>" is quite long.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=175282011-06-06T06:53:06Zcjheath (Clifford Heath)clifford.heath@gmail.com
<ul></ul><p>On 05/06/2011, at 11:29 PM, Benoit Daloze wrote:</p>
<blockquote>
<p>Clifford Heath wrote:</p>
<blockquote>
<p>That is, the problem is that it's not obvious to a newcomer that<br>
<code>__FILE__</code> means<br>
the current source-code file, or that <code>$0</code> means the name of the script<br>
being executed. Especially the latter...</p>
</blockquote>
<p><code>$PROGRAM_NAME</code> is an alias for <code>$0</code>.<br>
But "<code>if __FILE__ == $PROGRAM_NAME</code>" is quite long.</p>
</blockquote>
<p>Length is not a problem, if the text includes the meaning.</p>
<p>Things that are used often should be succinct, and the reader's<br>
knowledge should be assumed. Things that are used only a<br>
few times in a program do not need to be succinct.</p>
<p>To use an API call requires that the user knows (or looks up)<br>
the meaning. This kind of semantic hiding is completely<br>
unnecessary and <em>counter-productive</em>.</p>
<p>Clifford Heath.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=175322011-06-06T14:51:31Zlazaridis.com (Lazaridis Ilias)ilias@lazaridis.com
<ul></ul><p>Clifford Heath wrote:<br>
[...]</p>
<blockquote>
<blockquote>
<p>But "<code>if __FILE__ == $PROGRAM_NAME</code>" is quite long.<br>
[...]<br>
To use an API call requires that the user knows (or looks up)<br>
the meaning. This kind of semantic hiding is completely<br>
unnecessary and <em>counter-productive</em>.<br>
[...]</p>
</blockquote>
</blockquote>
<p>So maybe all rarely used methods should be written in long descriptive statements, to overcome non-semantic-hiding and become more productive?</p>
<p>There's one simple fact:</p>
<p>"<code>if __FILE__ == $PROGRAM_NAME</code>" is inconsistent with the language's elegant OO design - and I don't think that anyone will counter this.</p>
<p>And at this point, the "dispute" is about the method name.</p>
<p>Which method name would you choose, if you had two choices (<code>.main?</code> | <code>.executed?</code>) ?</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=175332011-06-06T15:23:07Zcjheath (Clifford Heath)clifford.heath@gmail.com
<ul></ul><p>On 06/06/2011, at 3:51 PM, Lazaridis Ilias wrote:</p>
<blockquote>
<p>So maybe all rarely used methods should be written in long<br>
descriptive statements, to overcome non-semantic-hiding and become<br>
more productive?</p>
</blockquote>
<p>I thought you prided yourself on your ability to use reason?<br>
Because the above is really an irrational statement, being<br>
both a logical non-sequitur and unrelated to my argument.</p>
<blockquote>
<p>There's one simple fact:<br>
"<code>if __FILE__ == $PROGRAM_NAME</code>" is inconsistent with the language's<br>
elegant OO design</p>
</blockquote>
<p>I think that's entirely false.</p>
<p>If I was explaining the intent of my code to you, I'd say<br>
"If we're running this file", or "if this is the file we're running".<br>
These have the same semantic structure as the Ruby code<br>
above... completely <em>unlike</em> the suggestion you make.</p>
<blockquote>
<p>And at this point, the "dispute" is about the method name.</p>
</blockquote>
<p>No, it isn't. It's about how to make Ruby easier to read.</p>
<blockquote>
<p>Which method name would you choose, if you had two choices (<code>.main?</code><br>
| <code>.executed?</code>) ?</p>
</blockquote>
<p>Neither. The meaning that is intended cannot be expressed using the<br>
natural meaning of any single word. If the method was implemented,<br>
I'd still write it out long-hand.</p>
<p>I want to minimise the <em>cognitive load</em> on people who read my code.</p>
<p>Clifford Heath.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=175402011-06-06T20:50:13Zlazaridis.com (Lazaridis Ilias)ilias@lazaridis.com
<ul></ul><p>Clifford Heath wrote:<br>
[...] - omitting, to focus on essence</p>
<blockquote>
<blockquote>
<p>And at this point, the "dispute" is about the method name.</p>
</blockquote>
<p>No, it isn't. It's about how to make Ruby easier to read.</p>
</blockquote>
<p>The status of this issue (<a class="issue tracker-2 status-2 priority-4 priority-default" title="Feature: Provide method Kernel#executed? (Assigned)" href="https://redmine.ruby-lang.org/issues/4824">#4824</a>) is "agreed providing a method" (= provide an OO construct) and more actual "We need a better name." (Source: See the first comment).</p>
<p>So, it's about to make the language easier to read, in context of an OO construct.</p>
<p>I've added some elaborations, subjecting the (OO method) name. Ideally, it would be one word, but it's possible to use something like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="n">this_file_is_equal_with_the_program_name?</span>
</code></pre>
<p>Although I dislike a two-word choice, the most logical seems to be this one:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="n">is_executed?</span> <span class="c1"># refers to the actual main object. the user has *anyway* to learn: there is a "self" behind, an object behind</span>
<span class="k">if</span> <span class="nb">self</span><span class="p">.</span><span class="nf">is_executed?</span> <span class="c1"># can be written with "self", to make it more clear</span>
<span class="n">main</span><span class="p">()</span> <span class="k">if</span> <span class="nb">self</span><span class="p">.</span><span class="nf">is_program?</span>
<span class="n">main</span><span class="p">()</span> <span class="k">if</span> <span class="n">is_program?</span>
</code></pre>
<p>So, the point is, to find a concise method name (ideally one word) which reads nice and fits in the overall existent naming scheme of the language.</p>
<p>(note that I "back-off" from this issue for now)</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=175412011-06-06T21:09:13Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Ruby allows a question mark in method names exactly for avoiding writing methods beggining with "<code>is_</code>" like in Java. I don't mind two word methods, but I don't want the first word to be a "<code>is</code>" when it is finished with a question mark. That is redundant.</p>
<p>By the way, among suggestions, I prefer either <code>main?</code> or <code>main_script?</code>. Maybe aliases :)</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=175902011-06-08T16:53:06Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>On Mon, Jun 6, 2011 at 7:09 AM, Rodrigo Rosenfeld Rosas<br>
<a href="mailto:rr.rosas@gmail.com" class="email">rr.rosas@gmail.com</a> wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-2 priority-4 priority-default" title="Feature: Provide method Kernel#executed? (Assigned)" href="https://redmine.ruby-lang.org/issues/4824">#4824</a> has been updated by Rodrigo Rosenfeld Rosas.</p>
<p>Ruby allows a question mark in method names exactly for avoiding writing methods beggining with "<code>is_</code>" like in Java. I don't mind two word methods, but I don't want the first word to be a "<code>is</code>" when it is finished with a question mark. That is redundant.</p>
<p>By the way, among suggestions, I prefer either <code>main?</code> or <code>main_script?</code>. Maybe aliases :)</p>
</blockquote>
<p>I like <code>main?</code> as well. But I have a concern: the method would have to<br>
be able to see the caller's context, along the lines of eval. I <em>hate</em><br>
the idea of adding more methods that can do that.</p>
<p>I might be more inclined to a keyword or pseudo constant along the<br>
lines of <code>__FILE__</code>, like <code>__MAIN__</code> that produces true iff <code>__FILE__ == $0</code>. Something we can statically determine before runtime without<br>
digging around in the caller's frame.</p>
<ul>
<li>Charlie</li>
</ul> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=175942011-06-08T18:23:06Zzenspider (Ryan Davis)
<ul></ul><p>On Jun 8, 2011, at 00:44 , Charles Oliver Nutter wrote:</p>
<blockquote>
<p>I might be more inclined to a keyword or pseudo constant along the<br>
lines of <code>__FILE__</code>, like <code>__MAIN__</code> that produces true iff <code>__FILE__ $0</code>. Something we can statically determine before runtime without<br>
digging around in the caller's frame.</p>
</blockquote>
<p>While I think the feature request is a bit inane to begin with, I think <code>__MAIN__</code> is a beautifully pragmatic compromise. Easy to implement on all impls, non-hacky, and yet makes its intention very clear.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=175972011-06-08T20:29:06Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Em 08-06-2011 06:13, Ryan Davis escreveu:</p>
<blockquote>
<p>On Jun 8, 2011, at 00:44 , Charles Oliver Nutter wrote:</p>
<blockquote>
<p>I might be more inclined to a keyword or pseudo constant along the<br>
lines of <code>__FILE__</code>, like <code>__MAIN__</code> that produces true iff <code>__FILE__ == $0</code>. Something we can statically determine before runtime without<br>
digging around in the caller's frame.<br>
While I think the feature request is a bit inane to begin with, I think <code>__MAIN__</code> is a beautifully pragmatic compromise. Easy to implement on all impls, non-hacky, and yet makes its intention very clear.</p>
</blockquote>
</blockquote>
<p>I like the idea too. I just think that Ruby is very simple to start with<br>
because it is very consistent and with a few rules. Someone would expect<br>
<code>__MAIN__</code> to be a constant, which is not. Maybe something like <code>__MAIN?</code><br>
would show the contrast to something that seems to be like a constant<br>
(<code>MAIN</code>) but doesn't seem at the same time (?). I know this is still<br>
confusing, but I prefer something like this instead of a pure <code>__MAIN__</code>...</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=175982011-06-08T20:31:55Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Actually, maybe something like the snippet below would be ideal:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="k">defined?</span><span class="p">(</span><span class="n">__MAIN__</span><span class="p">)</span> <span class="o">...</span>
</code></pre>
<p>This means <code>__MAIN__</code> is still a constant, but one defined by the interpreter in some conditional way that is injected only in a single file (the main one).</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=175992011-06-08T20:53:06Zspatulasnout (B Kelly)billk@cts.com
<ul></ul><p>Hi,</p>
<p>Rodrigo Rosenfeld Rosas wrote:</p>
<blockquote>
<p>I like the idea too. I just think that Ruby is very simple to start with<br>
because it is very consistent and with a few rules. Someone would expect<br>
<code>__MAIN__</code> to be a constant, which is not.</p>
</blockquote>
<p>Why would one expect <code>__MAIN__</code> to be any more or less of a<br>
constant than <code>__FILE__</code> or <code>__LINE__</code> or <code>__method__</code> ?</p>
<p>Seems to me its behavior would be consistent with the others.</p>
<p>Regards,</p>
<p>Bill</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=176032011-06-08T21:23:06Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Em 08-06-2011 08:50, Bill Kelly escreveu:</p>
<blockquote>
<p>Why would one expect <code>__MAIN__</code> to be any more or less of a<br>
constant than <code>__FILE__</code> or <code>__LINE__</code> or <code>__method__</code> ?</p>
<p>Seems to me its behavior would be consistent with the others.</p>
</blockquote>
<p>Hi Bill, yes, you're right. I agree with you. I didn't think about this<br>
before :)</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=176082011-06-08T23:29:06Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>Hi,</p>
<p>At Wed, 8 Jun 2011 16:44:55 +0900,<br>
Charles Oliver Nutter wrote in <a href="/issues/4824">[ruby-core:36833]</a>:</p>
<blockquote>
<p>I might be more inclined to a keyword or pseudo constant along the<br>
lines of <code>__FILE__</code>, like <code>__MAIN__</code> that produces true iff <code>__FILE__ == $0</code>. Something we can statically determine before runtime without<br>
digging around in the caller's frame.</p>
</blockquote>
<p><code>__FILE__.main?</code></p>
<p>--<br>
Nobu Nakada</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=176092011-06-08T23:53:06Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Em 08-06-2011 11:28, Nobuyoshi Nakada escreveu:</p>
<blockquote>
<p>Hi,</p>
<p>At Wed, 8 Jun 2011 16:44:55 +0900,<br>
Charles Oliver Nutter wrote in <a href="/issues/4824">[ruby-core:36833]</a>:</p>
<blockquote>
<p>I might be more inclined to a keyword or pseudo constant along the<br>
lines of <code>__FILE__</code>, like <code>__MAIN__</code> that produces true iff <code>__FILE__ == $0</code>. Something we can statically determine before runtime without<br>
digging around in the caller's frame.</p>
</blockquote>
<p><code>__FILE__.main?</code></p>
</blockquote>
<p>I loved this one!</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=176112011-06-09T01:13:46Zlazaridis.com (Lazaridis Ilias)ilias@lazaridis.com
<ul></ul><p>Nobuyoshi Nakada wrote:</p>
<blockquote>
<p>Hi,</p>
<p>At Wed, 8 Jun 2011 16:44:55 +0900,<br>
Charles Oliver Nutter wrote in <a href="/issues/4824">[ruby-core:36833]</a>:</p>
<blockquote>
<p>I might be more inclined to a keyword or pseudo constant along the<br>
lines of <code>__FILE__</code>, like <code>__MAIN__</code> that produces true iff <code>__FILE__ == $0</code>. Something we can statically determine before runtime without<br>
digging around in the caller's frame.</p>
</blockquote>
</blockquote>
<p>@C.O. Nutter<br>
I you dislike "digging around in the caller's frame", then you can possibly implement it in a different way.</p>
<blockquote>
<p><code>__FILE__.main?</code></p>
</blockquote>
<p>This is not an OO approach, even I would prefer to use "<code>if __FILE__ == $0</code>" instead.</p>
<p>I would expect to see <code>__FILE__.main?</code> in python, not in ruby.</p>
<p>There is already an object available, accessible via "<code>self</code>".</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">main?</span> <span class="c1"># read: is main?</span>
<span class="c1"># do main stuff</span>
<span class="k">end</span>
</code></pre>
<p>or</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">self</span><span class="p">.</span><span class="nf">main?</span> <span class="c1"># read: self is main?</span>
<span class="c1"># do main stuff</span>
<span class="k">end</span>
</code></pre>
<p>in some way, self refers to the file or code module/object.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=176122011-06-09T01:23:10Zmfn (Markus Fischer)markus@fischer.name
<ul></ul><p>Hi,</p>
<p>I take the courtesy to hijack this because ...</p>
<p>On 08.06.2011 13:31, Rodrigo Rosenfeld Rosas wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-2 priority-4 priority-default" title="Feature: Provide method Kernel#executed? (Assigned)" href="https://redmine.ruby-lang.org/issues/4824">#4824</a> has been updated by Rodrigo Rosenfeld Rosas.</p>
<p>Actually, maybe something like the snippet below would be ideal:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="k">defined?</span><span class="p">(</span><span class="n">__MAIN__</span><span class="p">)</span> <span class="o">...</span>
</code></pre>
<p>This means <code>__MAIN__</code> is still a constant, but one defined by the interpreter in some conditional way that is injected only in a single file (the main one).</p>
</blockquote>
<p>This "injected only in a single file" makes me wonder about one thing:<br>
does Ruby in some way provide a per file context?</p>
<p>I guess those familiar with Python will recognize this immediately, as a<br>
file is treated as a (python) module which has it's on scoping and<br>
provides the ability for others to "import" only certain features of a<br>
module/file you want.</p>
<p>I though it would give a nice addition to Ruby but as I understand it,<br>
it would be quite radical as Ruby has the module keyword already and<br>
things work (quite?) differently.</p>
<ul>
<li>Markus</li>
</ul> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=176132011-06-09T02:05:22Zlazaridis.com (Lazaridis Ilias)ilias@lazaridis.com
<ul></ul><p>Markus Fischer wrote:</p>
<blockquote>
<p>Hi,</p>
<p>I take the courtesy to hijack this because ...<br>
[...]</p>
</blockquote>
<p>Hijacking issues on an issue-tracking-system is really not the way to go.</p>
<p>Better open a new issue (or a new discussion topic) and place a link to it, if it's somehow related.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=176422011-06-10T02:30:47Zlazaridis.com (Lazaridis Ilias)ilias@lazaridis.com
<ul></ul><p>Lazaridis Ilias wrote:</p>
<blockquote>
<p>Nobuyoshi Nakada wrote:<br>
[...]</p>
<blockquote>
<p><code>__FILE__.main?</code></p>
</blockquote>
<p>This is not an OO approach, even I would prefer to use "<code>if __FILE__ == $0</code>" instead.</p>
<p>I would expect to see <code>__FILE__.main?</code> in python, not in ruby.</p>
<p>There is already an object available, accessible via "<code>self</code>".<br>
[...]</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">self</span><span class="p">.</span><span class="nf">main?</span> <span class="c1"># read: self is main?</span>
<span class="c1"># do main stuff</span>
<span class="k">end</span>
</code></pre>
<p>in some way, self refers to the file or code module/object.</p>
</blockquote>
<p>Correcting myself:</p>
<p>Taking in account that "<code>self</code>" refers to the "<code>main</code>" object (the global object) and not to the file object (as it should, from my point of view), possibly this one could do it:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">FILE</span><span class="p">.</span><span class="nf">executed?</span>
</code></pre>
<p>(this could get a related method: <code>FILE.imported?</code> )</p>
<p>Is there anything that disallows usage of "<code>FILE</code>" instead of "<code>__FILE__</code>"? I personally cannot look at those <code>__x__</code> things when writing OO (one reason I dropped python).</p>
<p>A convention like: CAPITALS for immutable constant objects (without those <code>__ __</code>)?</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=176472011-06-10T04:49:08ZCezary (Cezary Baginski)cezary.baginski@gmail.com
<ul></ul><p>This may seem like heresy, but isn't really:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="kp">__FILE__</span> <span class="o">==</span> <span class="vg">$0</span>
</code></pre>
<p>just a hack for letting a file be <em>both</em> a script and a "library" at the same time? With the only sane use (I can think of) being adding unit tests?</p>
<p>This was probably useful in the early years of Ruby, but now with the internet, social coding, methodologies, TDD, BDD, packaging (gems), etc. - doesn't it make more sense to have tests and scripts in separate files?</p>
<p>Why add a construct for handling a block of code that cannot be called in any other way, than running the script directly, creating dead code that isn't included in coverage?</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=176492011-06-10T07:20:30Zrocky (Rocky Bernstein)rocky@gnu.org
<ul></ul><p>Cezary Baginski wrote:</p>
<blockquote>
<p>This may seem like heresy, but isn't really:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="kp">__FILE__</span> <span class="o">==</span> <span class="vg">$0</span>
</code></pre>
<p>just a hack for letting a file be <em>both</em> a script and a "library" at the same time? With the only sane use (I can think of) being adding unit tests?</p>
</blockquote>
<p>I wrote my thoughts regarding this to ruby-core on June 5-6. See <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/36772" class="external">http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/36772</a>. But I realize that doesn't get reflected here in redmine.</p>
<p>It has one answer to your question which, in sum, is "demo code". Demonstration code is not the same as a unit test.</p>
<p>But there is another use. One can write a program that has a command-line interface, but folks can use this as a library instead. For example see, <a href="https://github.com/rocky/ps-watcher" class="external">https://github.com/rocky/ps-watcher</a></p>
<blockquote>
<p>This was probably useful in the early years of Ruby, but now with the internet, social coding, methodologies, TDD, BDD, packaging (gems), etc. - doesn't it make more sense to have tests and scripts in separate files?</p>
<p>Why add a construct for handling a block of code that cannot be called in any other way, than running the script directly, creating dead code that isn't included in coverage?</p>
</blockquote> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=176512011-06-10T09:08:43Zrbjl (Jan Lelis)hi@ruby.consulting
<ul></ul><p>I'd still prefer a Kernel method - It's about better readability, isn't it? My favourites:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">directly_executed?</span>
<span class="n">standalone?</span>
</code></pre>
<p>If it should be some kind of keyword, I can't see any serious issue against the <code>__MAIN__</code> solution. It's true in the "main" file, false in all others and</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="n">__MAIN__</span>
</code></pre>
<p>looks OK.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=176662011-06-10T21:53:14Zaustin (Austin Ziegler)halostatue@gmail.com
<ul></ul><p>On Thu, Jun 9, 2011 at 3:49 PM, Cezary Baginski<br>
<a href="mailto:cezary.baginski@gmail.com" class="email">cezary.baginski@gmail.com</a> wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-2 priority-4 priority-default" title="Feature: Provide method Kernel#executed? (Assigned)" href="https://redmine.ruby-lang.org/issues/4824">#4824</a> has been updated by Cezary Baginski.<br>
This may seem like heresy, but isn't really:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="kp">__FILE__</span>
</code></pre>
</blockquote> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=176812011-06-10T23:23:07ZCezary (Cezary Baginski)cezary.baginski@gmail.com
<ul></ul><p>On Fri, Jun 10, 2011 at 07:20:32AM +0900, Rocky Bernstein wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-2 priority-4 priority-default" title="Feature: Provide method Kernel#executed? (Assigned)" href="https://redmine.ruby-lang.org/issues/4824">#4824</a> has been updated by Rocky Bernstein.</p>
<p>Cezary Baginski wrote:</p>
<blockquote>
<p>This may seem like heresy, but isn't really:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="kp">__FILE__</span> <span class="o">==</span> <span class="vg">$0</span>
</code></pre>
<p>just a hack for letting a file be <em>both</em> a script and a "library"<br>
at the same time? With the only sane use (I can think of) being<br>
adding unit tests?</p>
</blockquote>
<p>I wrote my thoughts regarding this to ruby-core on June 5-6. See<br>
<a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/36772" class="external">http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/36772</a>.</p>
</blockquote>
<p>Yes, I read your comments (I read the list). It was what got me<br>
thinking. I didn't like checking using file path strings either.</p>
<blockquote>
<p>It has one answer to your question which, in sum, is "demo code".<br>
Demonstration code is not the same as a unit test.</p>
</blockquote>
<p>Yes, but shouldn't that be in a README or example.rb instead?</p>
<blockquote>
<p>But there is another use. One can write a program that has a<br>
command-line interface, but folks can use this as a library instead.<br>
For example see, <a href="https://github.com/rocky/ps-watcher" class="external">https://github.com/rocky/ps-watcher</a></p>
</blockquote>
<p>Shouldn't there be an executable script installed in bin/ loading a<br>
lib/ps-watcher/cli.rb file ? It would be accessible from <code>$PATH</code>.<br>
And unit/integration tests can be simpler and more robust.</p>
<p>I understand the use case(s), but I don't see why '<code>__FILE__ == $0</code>' is<br>
really that useful and good practice enough to be explicitly supported<br>
by the language.</p>
<p>As for the name - anything containing '<code>main</code>' assumes familiarity with<br>
C-type languages, which may or may not be the case for novices.</p>
<p>--<br>
Cezary Baginski</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=176822011-06-10T23:23:08ZCezary (Cezary Baginski)cezary.baginski@gmail.com
<ul></ul><p>On Fri, Jun 10, 2011 at 09:35:24PM +0900, Austin Ziegler wrote:</p>
<blockquote>
<p>On Thu, Jun 9, 2011 at 3:49 PM, Cezary Baginski<br>
<a href="mailto:cezary.baginski@gmail.com" class="email">cezary.baginski@gmail.com</a> wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-2 priority-4 priority-default" title="Feature: Provide method Kernel#executed? (Assigned)" href="https://redmine.ruby-lang.org/issues/4824">#4824</a> has been updated by Cezary Baginski.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="kp">__FILE__</span> <span class="o">==</span> <span class="vg">$0</span>
</code></pre>
<p>just a hack for letting a file be <em>both</em> a script and a "library"<br>
at the same time? With the only sane use (I can think of) being<br>
adding unit tests?</p>
</blockquote>
<p>Tests are most <em>certainly</em> not the only good reason for this feature,<br>
and I would regret its departure.</p>
</blockquote>
<p>By "sane" use I didn't mean "good" use. Especially with a team working<br>
together and source control. I have nothing against the current use of<br>
checking for main script, but I don't think promoting its use through<br>
additional language support is beneficial.</p>
<p>--<br>
Cezary Baginski</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=178982011-06-13T22:29:06ZCezary (Cezary Baginski)cezary.baginski@gmail.com
<ul></ul><p>On Sat, Jun 11, 2011 at 11:20:31AM +0900, Rocky Bernstein wrote:</p>
<blockquote>
<p>On Fri, Jun 10, 2011 at 10:03 AM, Cezary <a href="mailto:cezary.baginski@gmail.com" class="email">cezary.baginski@gmail.com</a> wrote:</p>
<blockquote>
<p>On Fri, Jun 10, 2011 at 07:20:32AM +0900, Rocky Bernstein wrote:</p>
<blockquote>
<p>It has one answer to your question which, in sum, is "demo code".<br>
Demonstration code is not the same as a unit test.</p>
</blockquote>
<p>Yes, but shouldn't that be in a README or example.rb instead?</p>
</blockquote>
<p>I prefer demo code to be runable in the same way that tests are runable. And<br>
for simple things, one file is better, feels more lightweight, and is less<br>
clumsy (...)</p>
</blockquote>
<p>So why not instead check for specific script arguments, like '--demo',<br>
'--test', '--help', etc.? If checking for '<code>__FILE__ == $0</code>' is really<br>
the best solution in a given case, my question is: why provide a<br>
special method for the same thing?</p>
<blockquote>
<p>I think we can tolerate different styles, no?</p>
</blockquote>
<p>Absolutely! So why not just use the following in such programs:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">main?</span><span class="p">;</span> <span class="kp">__FILE__</span> <span class="o">==</span> <span class="vg">$0</span><span class="p">;</span> <span class="k">end</span> <span class="c1"># for 'if main?'</span>
<span class="k">class</span> <span class="nc">String</span><span class="p">;</span> <span class="k">def</span> <span class="nf">main?</span><span class="p">;</span> <span class="nb">self</span> <span class="o">==</span> <span class="vg">$0</span><span class="p">;</span> <span class="k">end</span><span class="p">;</span> <span class="k">end</span> <span class="c1">#for '__FILE__.main?'</span>
</code></pre>
<p>That way, anyone can use their own style for the idiom with little<br>
coding overhead. And it will work with any Ruby version out there.</p>
<blockquote>
<blockquote>
<p>It would be accessible from <code>$PATH</code>.<br>
And unit/integration tests can be simpler and more robust.</p>
</blockquote>
<p>If you looked at the project, you'll see that there are tests. I don't see<br>
how unit/integration tests would be made simpler by putting the trivial<br>
command-line interface, or main() routine, in another file.</p>
</blockquote>
<p>Regarding the ps-watcher project specifically, it requires other<br>
project files anyway, so you cannot use the file standalone. So, in<br>
that case, you can assume most of the code (demo?) can be moved to<br>
other files, leaving only the "main" code, after which the '<code>__FILE__</code>'<br>
check around it can be removed altogether.</p>
<p>There is no README file, so it is a little hard to figure out how to<br>
use the application, without reading through everything. Replacing the<br>
"main checking" with a new Ruby 1.9.3 method (that would do the same) is<br>
something I find not worth the time to even discuss.</p>
<p>As for tests - there already is a Rakefile which runs them. Why not<br>
add a 'demo' task there as well?</p>
<p>I'm not trying to criticize the project here - I just don't see it as<br>
a convincing example showing how a new method for the idiom is<br>
valuable.</p>
<blockquote>
<p>Could it just be sufficient to accept that others find it useful<br>
even if it is not useful to you?</p>
</blockquote>
<p>Sure, the idiom is useful, even if I have a hard time thinking of a<br>
case where its use is elegant, the best practice and recommended -<br>
enough to explain the absolute need for anything more that the way it<br>
is done currently (with <code>__FILE__ == $0</code>).</p>
<blockquote>
<p>This is now feels like discussions in Perl when folks would ask if "until"<br>
was really important enough when there was "if" and "not" And if the form<br>
with "statement if expression" was really warranted since you had "if<br>
expression ...".</p>
</blockquote>
<p>I get what you are trying to say, but the example isn't a good one<br>
IMHO - keywords are much harder to emulate without language support,<br>
because they affect syntax. Things like Ruby's "<code>unless</code>" are valuable<br>
because of that. <code>__FILE__.main?</code> OTOH is an easy one-liner.</p>
<p>Let me try one: some people mix paint with screwdrivers, because they<br>
have them handy (after opening the paint box). Why would we want to<br>
add "paint mixer" features to <em>every</em> screwdriver to support this?</p>
<p>Having a special method for a special case is like having <code>7.is_five?</code><br>
or <code>"y".is_yes?</code>. Extreme examples, I know, but why is <code>#main?</code> an<br>
exception here?</p>
<blockquote>
<p>If you feel strongly about such matters, I suppose you could write a new<br>
programming language or perhaps carve out a subset of Ruby that does not<br>
have such things that you don't feel are useful or are good practice.</p>
</blockquote>
<p>To me this argument is along the lines of: "If you don't like adding<br>
very useful <code>is_mp3?</code> and <code>is_html?</code> methods to String, why don't you<br>
design your own programming language?".</p>
<p>IMHO, the idiom is not generic enough to deserve its own method.</p>
<p>Actually, it reminds me of discussions about Rails's<br>
<code>HashWithIndifferentAccess</code>. That class handles a specialized case which<br>
is very hard to do otherwise. I understand why it isn't necessary to<br>
support it in Ruby - and I think many will agree, but why in that case<br>
do we need a specialized method to replace code that already does a<br>
job just fine?</p>
<p>I would really like some clear guidelines for proposing and accepting<br>
similar methods in the future.</p>
<p>I don't really care if a method is added or not, I am really<br>
interested in: on what basis was it decided that it is worth<br>
considering?</p>
<p>Ideally, a reason that would be more definite than just a matter of<br>
taste or popularity. And without scrutiny, we may be missing possibly more<br>
sensible options. For example:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">require</span> <span class="s1">'main'</span>
<span class="no">Main</span><span class="p">.</span><span class="nf">run</span> <span class="k">do</span>
<span class="nb">puts</span> <span class="s2">"Running a demo"</span>
<span class="k">end</span>
</code></pre>
<p>That way we can even call the main manually, handle exit code,<br>
override, etc. Just an idea though.</p>
<p>--<br>
Cezary Baginski</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=179552011-06-14T20:23:06ZCezary (Cezary Baginski)cezary.baginski@gmail.com
<ul></ul><p>On Tue, Jun 14, 2011 at 03:23:27PM +0900, Rocky Bernstein wrote:</p>
<blockquote>
<p>On Mon, Jun 13, 2011 at 9:25 AM, Cezary <a href="mailto:cezary.baginski@gmail.com" class="email">cezary.baginski@gmail.com</a> wrote:</p>
<blockquote>
<p>So why not just use the following in such programs:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">main?</span><span class="p">;</span> <span class="kp">__FILE__</span> <span class="o">==</span> <span class="vg">$0</span><span class="p">;</span> <span class="k">end</span> <span class="c1"># for 'if main?'</span>
</code></pre>
</blockquote>
<p>Simplicity and unreliability.</p>
</blockquote>
<p>My first reaction is usually: is it valuable? Simple != valuable.</p>
<p>I think it is reliable enough for the cases mentioned. If reliability<br>
is an important issue here, then the implementation is more important<br>
than the name anyway. Unless the name is just a starting point for<br>
considering the issue at all.</p>
<p>Why not start with a gem first? Like <code>Object#me</code> (which became <code>#tap</code>) or<br>
<code>#andand</code> which IMHO is much more valuable but would greatly benefit from<br>
parser support in Ruby.</p>
<p>If simplicity is the main criteria, we could end up with Ruby's<br>
namespace exploding with "simplifying" methods that almost no one will<br>
use for various reasons. Why not create a 'main' gem and work on<br>
getting <code>#andand</code> (<code>#._?</code>) support in Ruby's parser instead?</p>
<p><code>#tap</code> turned out awesome IMHO. I don't see <code>#main?</code> as revolutionary.</p>
<blockquote>
<p>Everything you suggest, adds more code. I want less boilerplate code in<br>
fewer files. That is what I meant by "lightweight".</p>
</blockquote>
<p>Modularity and more code-sharing friendliness is more important. The<br>
ps-watcher project seems to reflect this - it contains more than one<br>
file, tests separate, Rakefile, functionality split up into small *.rb<br>
files, etc. If you like, I can spend some time to see what ps-watcher<br>
would like without the 'main' check. Not as a criticism, just as a way<br>
to support my point regarding design.</p>
<p>I think it would be great to first have a 'main' gem until the<br>
implementation matures. And it could be used in older Ruby<br>
applications immediately. Like <code>#tap</code>, <code>#andand</code>, etc.</p>
<blockquote>
<p>Early on in the thread, Matz had said he was amenable to the idea of adding<br>
a method. But he was unsure about the name. If he had indicated he wasn't<br>
interested, I would have dropped the topic and never have posted a response<br>
initially.</p>
</blockquote>
<p>Exactly! But I'm still not convinced about the value of such a method.<br>
I know I'm dumb, but I don't think I'm dumb enough to not understand a<br>
simple, valuable use case where a method is really an improvement<br>
worth adding and backporting. Or why wasn't this ticket immediately<br>
rejected.</p>
<p>Initially, I assumed I'm an idiot and believed the experts on this<br>
list saw the value, which I couldn't. Being interested in improving my<br>
skills, I got curious to learn what I am not seeing.</p>
<p>There is so much functionality that doesn't belong in Ruby core that<br>
is way more valuable. How did this get anything else than "rejected"?<br>
My only guess is unfortunate popularity of a use case that in itself<br>
suggests bad design - which is why an alarm in my head went off.</p>
<blockquote>
<p>In my first post which you said I read, I also discussed why the idiom is<br>
unreliable.</p>
</blockquote>
<p>Yes, and I can imagine it being a problem fixable with a well thought<br>
out implementation. It is good you brought it up.</p>
<p>I just don't see why improving the reliability of such a minor issue<br>
(IMHO) is really productive and worth any other reaction than<br>
rejecting or at least suggesting a new gem first.</p>
<p>Sorry for prolonging this discussion - I believe it may be worth<br>
learning to deal with this issue (or even just people like me) and<br>
preventing a whole class of similar cases (discussions?) in the<br>
future.</p>
<p>If my issues are pointless - let me know and I'll put in more trust in<br>
faith in the experts reading on ruby-core and give up on trying to<br>
change the way I think.</p>
<p>Thanks!</p>
<p>--<br>
Cezary Baginski</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=179912011-06-16T06:29:09Zsandal (Gregory Brown)gregory.t.brown@gmail.com
<ul></ul><p>On Sat, Jun 4, 2011 at 10:17 AM, Yukihiro Matsumoto <a href="mailto:matz@ruby-lang.org" class="email">matz@ruby-lang.org</a> wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-2 priority-4 priority-default" title="Feature: Provide method Kernel#executed? (Assigned)" href="https://redmine.ruby-lang.org/issues/4824">#4824</a> has been updated by Yukihiro Matsumoto.</p>
<p>I agree providing a method to tell whether it is loaded as a library or is executed as a command is more elegant than '<strong>FILE</strong></p>
</blockquote> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=180812011-06-19T01:23:58Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>On Wed, Jun 15, 2011 at 4:24 PM, Gregory Brown<br>
<a href="mailto:gregory.t.brown@gmail.com" class="email">gregory.t.brown@gmail.com</a> wrote:</p>
<blockquote>
<p>Lastly, I think <code>__MAIN__</code> is reasonable if a method is not to be used,<br>
though it feels a bit too magic for me.</p>
</blockquote>
<p>I'll repeat what others have repeated...more magic than <code>__FILE__</code>,<br>
<code>__LINE__</code>, or even <code>$0</code> itself?</p>
<ul>
<li>Charlie</li>
</ul> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=181672011-06-23T15:20:07Zlazaridis.com (Lazaridis Ilias)ilias@lazaridis.com
<ul></ul><p>The sentence "defines" essentially the terminology:</p>
<p>Yukihiro Matsumoto wrote:</p>
<blockquote>
<p>I agree providing a method to tell whether it is loaded as a library or<br>
^^^^^^^^^<br>
"is loaded"</p>
</blockquote>
<p>loaded?</p>
<blockquote>
<p>is executed as a command<br>
^^^^^^^^^^^<br>
"is executed"</p>
</blockquote>
<p>executed?</p>
<blockquote>
<p>is more elegant than '<code>__FILE__ == $0</code>', but I am still afraid #executed? can mean multiple ways so that the name can cause confusion. We need a better name.</p>
</blockquote>
<p>Thus the question to focus on is:</p>
<ul>
<li>is "<code>executed?</code>" not clear enough?</li>
</ul>
<p>If not, possibly just "<code>loaded?</code>" can be introduced.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">main</span><span class="p">()</span> <span class="k">unless</span> <span class="n">loaded?</span>
</code></pre> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=251302012-03-25T16:06:12Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=270762012-06-08T05:12:32Zfxn (Xavier Noria)fxn@hashref.com
<ul></ul><p>I vote for <code>__MAIN__</code>.</p>
<p>I think "main" will resonate to a lot of people with different backgrounds to indicate the entry point of the program, and choosing a keyword instead of a method seems coherent with other keywords like <code>__FILE__</code>.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=333092012-11-20T22:58:03Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Target version</strong> set to <i>2.6</i></li></ul> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=512162015-01-25T22:18:41Zsawa (Tsuyoshi Sawada)
<ul></ul><p>Close to Nobu's proposal <code>FILE.main?</code>, but I don't think it makes sense to introduce two new things <code>FILE</code> and <code>main?</code> that can only be used under this combination. I propose defining a method on the existing <code>File</code> class. Something along:</p>
<pre><code>File.main?
File.from_here?
</code></pre>
<p>Edit: Seeing Nobu's comment 39, I realize it was my mistake. Sorry.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=512182015-01-25T23:29:04Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/51218/diff?detail_id=36874">diff</a>)</li></ul><p>My proposal was <code>__FILE__.main?</code>, not <code>FILE.main?</code>.<br>
Just you're confused by markdown.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=512272015-01-26T18:25:19Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>The "main" toplevel object already gets special methods, so why not just define main.main? Avoid polluting Kernel or creating a special class for __FILE__ just to support a "main" feature.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=686882017-12-25T18:15:00Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> deleted (<del><i>2.6</i></del>)</li></ul> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=689612017-12-26T07:22:48Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>The names (or APIs) suggested so far:</p>
<pre><code>executed?
required?
main?
first_script?
main_script?
run_script?
is_executed?
is_program?
defined?(__MAIN__)
__FILE__.main?
FILE.executed?
directly_executed?
standalone?
loaded?
__MAIN__
File.main?
File.from_here?
main.main?
</code></pre> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=699552018-01-29T05:30:51Znaofumi-fujii (naofumi fujii)
<ul></ul><p>Hi, i created a patch for this ticket.<br>
please take a look.</p>
<p><a href="https://github.com/ruby/ruby/pull/1802" class="external">https://github.com/ruby/ruby/pull/1802</a></p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=707742018-03-04T14:25:25Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>It seems as if this may be decided at the upcoming ruby developer<br>
meeting.</p>
<p>Matz voiced concern about the suggested name. Yusuke Endoh provided<br>
a list of alternative names (or rather, a summary). I assume that the<br>
name for the functionality here is the primary concern; the functionality<br>
I assume is fine, so let's have a look at <strong>possible names</strong>.</p>
<p>I myself currently use this:</p>
<pre><code>if __FILE__ == $PROGRAM_NAME
</code></pre>
<p>The reason why I use the longer $PROGRAM_NAME is because it tells<br>
me more than $0. $0 really does not tell me anything; the only<br>
advantage $0 has is that it is short. But a method call would be<br>
even shorter.</p>
<p>The name "executed?" is not good, I think, because it implies<br>
a state that has already been passed, in the past (that is<br>
my interpretation, as a non-native english speaker).</p>
<p>I like the name <strong>run_script?</strong> - although it may not be a<br>
100% fitting name (what is a "script" for example), it has a<br>
meaning that tells me something. Perhaps <strong>run_file?</strong> may be<br>
a better name, but I am not absolutely sure either. What I<br>
like about this is that it is more explicit and conveys some<br>
meaning to me when I look at code.</p>
<p>I don't like the name <strong>main.main?</strong> because of the repetition.<br>
It seems superfluous to repeat it; and "main?" also tells me<br>
nothing really.</p>
<p>I don't like __ MAIN __ much either because of the leading and<br>
trailing __. I understand that ruby uses it already internally<br>
and we use it via <strong>FILE</strong> == comparisons but I do not think<br>
that __ MAIN __ is any improvement over <strong>FILE</strong> ==</p>
<p>Sorry for splitting the above, <strong>markdown</strong> appears to think that<br>
__ means some bold emphasis.</p>
<p>defined?(<strong>MAIN</strong>) is bad because it requires too much typing<br>
and is not a substantial improvement over <strong>FILE</strong> ==</p>
<p>I also do not like the calls to "File.method" such as<br>
FILE.executed? - calls to Kernel are better, IMO.</p>
<p>Anyway, I don't want to inflate this too much so I will add some<br>
names as suggestions - I will focus on something that may have a<br>
meaning from the name alone, and assumingly resides on the<br>
Kernel namespace; I'll also update on mame's list, at the<br>
least those entries that I think make for better names:</p>
<p>(I think I prefer names that refer to "standalone" and "run"<br>
or something along those lines.)</p>
<pre><code>is_standalone?
run_standalone?
standalone?
is_executed?
is_main?
run_script?
run_file?
shall_run?
is_runnable?
</code></pre>
<p>I prefer the first two variants:</p>
<p><strong>is_standalone?</strong> or <strong>run_standalone?</strong></p>
<p>They would convey that the file may be run "standalone",<br>
that is, "on its own" - similar to the current check that<br>
I (and others) do such as via:</p>
<pre><code>if __FILE__ == $PROGRAM_NAME
end
</code></pre>
<p>Anyway, if we ignore any specific name, perhaps if we can<br>
not narrow it down to just one name, we (or the ruby core<br>
team at a meeting) could pick three variants, and we could<br>
use all three as experimental feature, before deciding on<br>
the primary way and removing the others (or keeping them<br>
as aliases; but I think, although I love aliases, it may<br>
be better to use only one way here, for a method, so that<br>
people all use the same in their code. I love aliases but<br>
in this case, I think it is better to encourage only the<br>
best name. When we have e. g. 3 possible names, I am sure<br>
it will be much easier to select the best name altogether<br>
after a while of ruby hackers using these names; and it<br>
could be an experimental feature before the xmas release,<br>
upon where it could be finalized and the best one decided.<br>
Or matz just decides on a name, that may be the shortest<br>
and simplest perhaps. :D My primary hope here is that<br>
the name is a good name which is used a lot - it would<br>
not be good if people prefer "if <strong>FILE</strong> == $PROGRAM_NAME"<br>
because they dislike the new name)</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=707752018-03-04T16:45:15Zdsferreira (Daniel Ferreira)
<ul></ul><p>Why not:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Kernel</span><span class="c1">#executable?</span>
<span class="c1"># => true if FILE == $0</span>
</code></pre>
<p>meaning: Is the file good to be executed?</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=707762018-03-04T22:39:52Zgraywolf (Gray Wolf)
<ul></ul><p>I'm not a native English speaker, so this whole comment might be completely off,<br>
but at least to me the ending <code>~able?</code> implies that it is possible to use it<br>
that way, not necessarily that is was used that way.</p>
<p>So <code>Kernel#executable?</code> tells me that something is able to be executed, not that<br>
it was in fact executed. <code>is_standalone?</code> imho suffers from the same thing (btw<br>
why not just <code>standalone?</code>).</p>
<p>At least to me <code>Kernel#executed?</code> looks better.</p>
<p>But I like <code>__MAIN__</code> or <code>Kernel#main?</code> the best. Anyone who ever saw C or Python<br>
would be able to guess what it means even without checking documentation.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=707772018-03-04T23:12:46Zdsferreira (Daniel Ferreira)
<ul></ul><p>graywolf (Gray Wolf) wrote:</p>
<blockquote>
<p>So Kernel#executable? tells me that something is able to be executed, not that<br>
it was in fact executed.</p>
</blockquote>
<p>Correct, <code>Kernel#executable?</code> infers if the code is able to be executed.</p>
<p>Usually when we use</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="kp">__FILE__</span> <span class="o">==</span> <span class="vg">$0</span>
<span class="c1"># call code to be run</span>
<span class="n">main</span><span class="p">(</span><span class="no">ARGV</span><span class="p">)</span>
<span class="k">end</span>
</code></pre>
<p>We are saying that the code should only be executed if the condition applies.</p>
<p>The code will be executed once we call <code>main(ARGV)</code>.<br>
The code is able to be executed if <code>__FILE__ == $0</code>.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=707782018-03-04T23:18:43Zgraywolf (Gray Wolf)
<ul></ul><p>dsferreira (Daniel Ferreira) wrote:</p>
<blockquote>
<p>[..]</p>
<p>Usually when we use</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="kp">__FILE__</span> <span class="o">==</span> <span class="vg">$0</span>
<span class="c1"># call code to be run</span>
<span class="n">main</span><span class="p">(</span><span class="no">ARGV</span><span class="p">)</span>
<span class="k">end</span>
</code></pre>
<p>We are saying that the code should only be executed if the condition applies.</p>
<p>[..]</p>
</blockquote>
<p>Exactly, and the condition is: <em>was this file directly</em> <strong>executed?</strong>.</p>
<p>Or: <em>is this the</em> <strong>main?</strong> <em>script</em><br>
Or: <em>is this the</em> <strong>main_script?</strong></p>
<p>I mean, I get what you mean, but still think it's a bit confusing.</p>
<p>EDIT:</p>
<p>Given these two snippets in pseudo-code, which better shows the intent of the<br>
<code>if</code>?</p>
<pre><code>if this file is the main script
do main stuff
end
</code></pre>
<p>or</p>
<pre><code>if the code below is able to be executed
do main stuff
end
</code></pre>
<p>Sure, both do the same thing but I think the first one shows the intent better.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=707792018-03-04T23:26:18Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>For what it's worth, I think <code>__main__</code> is best, or <code>main?</code> if we're moving away from underscores for source-level metacode.</p>
<p>"Executable" is such an overloaded term, and "executed" is weird (isn't all code executed?); the convention of a single "main" entry point has been around for a long time now.</p> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=707802018-03-04T23:26:26Zdsferreira (Daniel Ferreira)
<ul></ul><p>graywolf (Gray Wolf) wrote:</p>
<blockquote>
<p>Exactly, and the condition is: was this file directly executed?.</p>
</blockquote>
<p>We have two different contexts:</p>
<ul>
<li>File context (FILE#executed?) - Was the file executed?</li>
<li>Code context (<code>Kernel#executable?</code>) - Is the code executable?</li>
</ul>
<p>I prefer to work at the code context level since we can then add other conditions to allow a code to be executable.<br>
e.g.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">executable?</span>
<span class="kp">__FILE__</span> <span class="o">==</span> <span class="vg">$0</span> <span class="o">&&</span> <span class="no">ARGV</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s2">"foo"</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=707812018-03-04T23:33:33Zdsferreira (Daniel Ferreira)
<ul></ul><p>phluid61 (Matthew Kerwin) wrote:</p>
<blockquote>
<p>For what it's worth, I think <strong>main</strong> is best, or main?</p>
</blockquote>
<p>I would agree with <code>main</code> in the following way:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="c1"># Kernel#main method to be overridden and only run if __FILE__ == $0</span>
<span class="k">def</span> <span class="nf">main</span>
<span class="no">Foo</span><span class="p">.</span><span class="nf">bar</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=707822018-03-04T23:40:17Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>dsferreira (Daniel Ferreira) wrote:</p>
<blockquote>
<p>phluid61 (Matthew Kerwin) wrote:</p>
<blockquote>
<p>For what it's worth, I think <strong>main</strong> is best, or main?</p>
</blockquote>
<p>I would agree with <code>main</code> in the following way:</p>
</blockquote>
<p><code>__main__</code>, not <strong>main</strong>. Returns a boolean value, which can be used as a predicate:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="n">__main__</span>
<span class="n">foo</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #4824: Provide method Kernel#executed?https://redmine.ruby-lang.org/issues/4824?journal_id=707832018-03-05T04:57:12Zsawa (Tsuyoshi Sawada)
<ul></ul><p>From the code <code>__FILE__ == $0</code>, we can tell that what matters is that this condition is within the main file that is executed. If we are to write something like <code>if __main__</code> or <code>if main?</code>, I have a concern that it will become less clear that that condition has to be placed within the main file. Some (careless) users might take it, for example, that such condition becomes false when it is a part of a gem, and becomes true when it is not part of the gem, irrespective of which file that condition is placed.</p>
<p>In order to avoid such problems, I think a method name like <code>main_file?</code> would be better. Also, in order to give flexibility so that the condition can be placed in files other than the main file, it might be good to allow an optional argument as in <code>main_file?(file_name)</code>, where <code>file_name</code> defaults to <code>__FILE__</code>.</p>