Issue #4824 has been updated by Rocky Bernstein.


Cezary Baginski wrote:
> This may seem like heresy, but isn't really:
> 
>   __FILE__ == $0
> 
> just a hack for letting a file be *both* a script and a "library" at the same time? With the only sane use (I can think of) being adding unit tests?

I wrote my thoughts regarding this to ruby-core on June 5-6. See http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/36772. But I realize that doesn't get reflected here in redmine.

It has one answer to your question which, in sum, is "demo code".  Demonstration code is not the same as a unit test. 

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, https://github.com/rocky/ps-watcher

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


----------------------------------------
Feature #4824: Provide method Kernel#executed?
http://redmine.ruby-lang.org/issues/4824

Author: Lazaridis Ilias
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 


The current construct to execute main code looks not very elegant:

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

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

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

or

main() if executed?

This addition would not break any existent behaviour.



-- 
http://redmine.ruby-lang.org