Issue #4824 has been updated by shevegen (Robert A. Heiler).


It seems as if this may be decided at the upcoming ruby developer
meeting.

Matz voiced concern about the suggested name. Yusuke Endoh provided
a list of alternative names. I assume that the name is the primary
concern; the functionality I assume is fine, so let's have a look
at possible names.

I myself currently use this:

    if __FILE__ == $PROGRAM_NAME

The reason why I use the longer $PROGRAM_NAME is because it tells 
me more than $0. $0 really does not tell me anything; the only
advantage $0 has is that it is short. But a method call would be
even shorter.

The name "executed?" is not good, I think, because it implies
a state that has already been passed, in the past (that is
my interpretation, as a non-native english speaker).

I like the name **run_script?** - although it may not be a
100% fitting name (what is a "script" for example), it has a
meaning that tells me something.

I don't like the name **main.main?** because of the repetition.
It seems superfluous to repeat it; and "main?" also tells me
nothing really.

I don't like __MAIN__ much either because of the leading and
trailing __. I understand that ruby uses it already internally
and we use it via __FILE__ == comparisons but I do not think
that __MAIN__ is any improvement over __FILE__ ==

defined?(__MAIN__) is bad because it requires too much typing
and is not a substantial improvement over __FILE__ ==

I also do not like the calls to "File.method" such as
FILE.executed? - calls to Kernel are better, IMO.

Anyway, I don't want to inflate this too much so I will add some
names as suggestions - I will focus on something that may have a
meaning from the name alone, and assumingly resides on the
Kernel namespace; I'll also update on mame's list, at the
least those entries that I think make for better names:

(I think I prefer names that refer to "standalone" and "run"
or something along those lines.)

is_standalone?
run_standalone?
standalone?
is_executed?
is_main?
run_script?
run_file?
shall_run?
is_runnable?

I prefer the first two variants: is_standalone?
or run_standalone?

They would convey that the file may be run "standalone",
that is, "on its own" - similar to the current check that
I (and others) do such as via:

    if __FILE__ == $PROGRAM_NAME
  
    end

Anyway, if we ignore any specific name, perhaps if we can
not narrow it down to just one name, we (or the ruby core
team at a meeting) could pick three variants, and we could
use all three as experimental feature, before deciding on
the primary way and removing the others (or keeping them
as aliases; but I think, although I love aliases, it may
be better to use only one way here, for a method, so that
people all use the same in their code. I love aliases but
in this case, I think it is better to encourage only the
best name. When we have e. g. 3 possible names, I am sure
it will be much easie to select the best name altogether;
and it could be an experimental feature before the xmas
release, upon where it could be finalized.)

----------------------------------------
Feature #4824: Provide method Kernel#executed?
https://bugs.ruby-lang.org/issues/4824#change-70774

* Author: lazaridis.com (Lazaridis Ilias)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
The current construct to execute main code looks not very elegant:

~~~ruby
if __FILE__ == $0
  my_main()          # call any method or execute any code
end
~~~

With a `Kernel#executed?` method, this would become more elegant:

~~~ruby
if executed?
  #do this
  #do that
  my_main()
end
~~~

or

~~~ruby
main() if executed?
~~~

This addition would not break any existent behaviour.




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>