Issue #4824 has been updated by sawa (Tsuyoshi Sawada).


From the code `__FILE__ == $0`, 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 `if __main__` or `if main?`, 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.

In order to avoid such problems, I think a method name like `run_from_this_file?`, or a method that takes the file name as an argument `main_file?(__FILE__)` would be better.

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

* 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>