Issue #4824 has been updated by Lazaridis Ilias.


Lazaridis Ilias wrote:
> Nobuyoshi Nakada wrote:
[...]
> >  __FILE__.main?
> 
> This is not an OO approach, even I would prefer to use "if __FILE__ == $0" instead.
> 
> I would expect to see __FILE__.main? in python, not in ruby.
> 
> There is already an object available, accessible via "self".
[...] 
> self.main? # read: self is main?
>  # do main stuff
> end
> 
> in some way, self refers to the file or code module/object.

Correcting myself:

Taking in account that "self" refers to the "main" object (the global object) and not to the file object (as it should, from my point of view), possibly this one could do it:

FILE.executed?

(this could get a related method: FILE.imported? )

Is there anything that disallows usage of "FILE" instead of "__FILE__"? I personally cannot look at those __x__ things when writing OO (one reason I dropped python).

A convention like: CAPITALS for immutable constant objects (without those __ __)?
----------------------------------------
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