From: Rocky Bernstein Date: 2011-06-06T07:04:53+09:00 Subject: [ruby-core:36772] Re: [Ruby 1.9 - Feature #4824] Provide method Kernel#executed? --0015174ff252fec85204a4fe2c74 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Jun 5, 2011 at 5:33 PM, Clifford Heath wrote: > On 05/06/2011, at 11:29 PM, Benoit Daloze wrote: > >> Clifford Heath wrote: >> >>> That is, the problem is that it's not obvious to a newcomer that __FILE__ >>> means >>> the current source-code file, or that $0 means the name of the script >>> being executed. Especially the latter... >>> >> >> $PROGRAM_NAME is an alias for $0. >> But "if __FILE__ == $PROGRAM_NAME" is quite long. >> > > Length is not a problem, if the text includes the meaning. > > Things that are used often should be succinct, and the reader's > knowledge should be assumed. Things that are used only a > few times in a program do not need to be succinct. > > To use an API call requires that the user knows (or looks up) > the meaning. This kind of semantic hiding is completely > unnecessary and *counter-productive*. > In my opinion, the reason for adding executed? or something like this is because __FILE__ == $PROGRAM_NAME does not reliably test whether a program has been invoked directly from Ruby or is required/loaded. The sources of variability (and thus unreliability) stem from *both* sides of the "==". First, __FILE__ can be a relative file name. Consider this code in two files /tmp/foolme.rb and /tmp/foolme/foolme.rb: # File /tmp/foolme.rb puts "++1 #{__FILE__}" Dir.chdir('/tmp/foolme') { require './foolme' } # File /tmp/foolme/foolme.rb puts "++2 #{__FILE__}" If you run this on MRI 1.8 or Rubinius 1.8 (e.g. 1.2.3) you will get this output: ++1 ./foolme.rb ++2 ./foolme.rb On MRI 1.9 the output is different. On the other side of the ==, you have the problem that $0 or $PROGRAM_NAME can be set. Thus, if it is useful to be able to tell if a file has been invoked initially as main script, then it would be nice have a reliable and way to do so. I think it useful to distinguish the two in order to more reliably support the style of programming I use. I generally put little demo code, at the bottom of each class or module. You can think of this demo code as something like unit test code; often it is modified and becomes unit test code. But demo code and test code have a slightly different focus, since demoing and testing are different. > Clifford Heath. > > > --0015174ff252fec85204a4fe2c74 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Sun, Jun 5, 2011 at 5:33 PM, Clifford Heath <clifford.heath@gmail.com> wrote:
On 05/06/2011, at 11:29 PM, Benoit Daloze wrote:
Clifford Heath wrote:
That is, the problem is that it's not obvious to a newcomer that __FILE= __ means
the current source-code file, or that $0 means the name of the script
being executed. Especially the latter...

$PROGRAM_NAME is an alias for $0.
But "if __FILE__ =3D=3D $PROGRAM_NAME" is quite long.

Length is not a problem, if the text includes the meaning.

Things that are used often should be succinct, and the reader's
knowledge should be assumed. Things that are used only a
few times in a program do not need to be succinct.

To use an API call requires that the user knows (or looks up)
the meaning. This kind of semantic hiding is completely
unnecessary and *counter-productive*.

I= n my opinion, the reason for adding =A0executed? or something like this is = because __FILE__ =3D=3D $PROGRAM_NAME=A0does not reliably test whether a pr= ogram has been invoked directly from Ruby or is required/loaded.

The sources of=A0variability (and thus unreliability) s= tem from *both* sides of the "=3D=3D". =A0First, __FILE__ can be = a relative file name. Consider this code in two files /tmp/foolme.rb and /t= mp/foolme/foolme.rb:=A0

# File /tmp/foolme.rb
puts "++1 #{_= _FILE__}"=A0
Dir.chdir('/tmp/foolme') { require '= ;./foolme' }

# File /tmp/foolme/foolme.r= b
puts "++2 #{__FILE__}"

I= f you run this on MRI 1.8 or Rubinius 1.8 (e.g. 1.2.3) you will get this ou= tput:

++1 ./foolme.rb
++2 ./foolme.= rb

On MRI 1.9 the output is different.=A0
=
On the other side of the =3D=3D, you have the problem that $= 0 or $PROGRAM_NAME can be set.

Thus, if it is usef= ul to be able to tell if a file has been invoked initially as main script, = then it would be nice have a reliable and way to do so.=A0

I think it useful to distinguish the two=A0in order to = more reliably support the style of programming I use. =A0I generally put li= ttle demo code, at the bottom of each class or module. You can think of thi= s demo code as something like unit test code; often it is modified and beco= mes unit test code. But demo code and test code have a slightly different f= ocus, since demoing and testing are different.=A0



Clifford Heath.



--0015174ff252fec85204a4fe2c74--