Just because “if !$INCLUDED” would read so much better than “if FILE == $0”
(Just googled, and this has come up before - ruby-talk 21504. However,
no one came up with a really convincing name, so I think the thread’s
worth reprising).
In message “RCR: $INCLUDED global var” > on 03/05/16, Martin DeMello martindemello@yahoo.com writes:
$INCLUDED = (FILE != $0)
Just because “if !$INCLUDED” would read so much better than “if FILE == $0”
But a global variable is still ugly for something new. I like the
idea of having alternative condition. But it should appear nicer,
I think.
–
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================
At Fri, 16 May 2003 04:17:58 +0900, Joel VanderWerf wrote:
$INCLUDED = (FILE != $0)
Just because “if !$INCLUDED” would read so much better than “if FILE == $0”
But a global variable is still ugly for something new. I like the
idea of having alternative condition. But it should appear nicer,
I think.
“Yukihiro Matsumoto” matz@ruby-lang.org wrote in message news:1053047645.721520.9976.nullmailer@picachu.netlab.jp…
Hi,
LIB ?
LIB → FILE != $0, eg. we have been included.
“include” is the C term, we use “require”. Anyway, I think the
condition should be reversed, since we check that way.
What about
MAIN {
# …
}
It’s sorta analogous with BEGIN and END.
Better. But I still want better name.
matz.
Seems like the form of Joel’s submission was very close.
Something like … ?
DEMO {
…
}
Could it return true/false for Nobu … ?
unless DEMO {}
… with or without a block.
The way I use it is to DEMOnstrate what the lib can do.
DEMO is informal, but not as informal as ‘if FILE == $0’
and more formal IMHO than, for example, ‘chomp’ ;-))
Better suggestions will, hopefully, follow.
$R = true
def dEMO # pseudo-DEMO block
if $R # simulate FILE != $0 (i.e. here due to ‘require’)
false
else
yield if block_given?
true
end
end # DEMO block returns true or false ONLY
···
In message “Re: RCR: $INCLUDED global var” > on 03/05/16, ahoward ahoward@fsl.noaa.gov writes:
In message “Re: RCR: $INCLUDED global var” > on 03/05/16, Joel VanderWerf vjoel@PATH.Berkeley.EDU writes:
######################################
2.times do
$R = !$R # reverse condition
if $R
puts “FILE != $0 (i.e. by require ‘xxxxx’)”
else
puts ‘FILE == $0 (i.e. NOT by require)’
end
puts
#--------- Simulating this code in either condition
print "DEMO = ", dEMO {}, "\n"
dEMO do
# pseudo-DEMO block
# require 'xxxxx'
puts 'Only run if NOT require-d'
end
puts "Run if by require 'xxxxx'" unless dEMO {p '... more DEMO'}
unless dEMO
puts 'This is the Real Thing'
end
#---------
puts ‘’, ‘-’ * 50, ‘’
end
FILE == $0 (i.e. NOT by require)
DEMO = true
Only run if NOT require-d
“… more DEMO”
FILE != $0 (i.e. by require ‘xxxxx’)
DEMO = false
Run if by require ‘xxxxx’
This is the Real Thing
“include” is the C term, we use “require”. Anyway, I think the
condition should be reversed, since we check that way.
What about
MAIN {
# …
}
It’s sorta analogous with BEGIN and END.
Better. But I still want better name.
matz.
IF_THIS_FILE { #OK, it’s kind of klunky
#…
}
-OR-
TEST {
#…
}
I like the second one, because that’s what I’m usually using the code in
that secion for; unit testing a module or class(s) that appear above in
the file.
In message “Re: RCR: $INCLUDED global var” > on 03/05/16, ahoward ahoward@fsl.noaa.gov writes:
In message “Re: RCR: $INCLUDED global var” > on 03/05/16, Joel VanderWerf vjoel@PATH.Berkeley.EDU writes:
PROGRAM? (and LIBRARY for the other case, though I can’t think of
anything you’d want in there only if the code was 'require’d).
Actually, I like MAIN - it has a lot of precedent.
martin
···
On Fri, 16 May 2003, Yukihiro Matsumoto wrote:
In message “Re: RCR: $INCLUDED global var” > > on 03/05/16, Joel VanderWerf vjoel@PATH.Berkeley.EDU writes:
I like the second one, because that’s what I’m usually using the code in
that secion for; unit testing a module or class(s) that appear above in
^^^^^^^^^ What ?
You mean you don’t use the wonderful Test::Unit framework ! Shame !!
Ok, just kidding
I really like your suggestion … I do use it for the same reason. Hope it
gets accepted.
Phil
^^^^^ I like your TaskMaster Framework too
– shanko
After reading the second chunk of this thread (which
is fragmented in my newsreader because of dropped
references) and the previous archive discussion
referred to by the OP (Martin), I’ve reconsidered.
(FILE != $0) is a binary value, constant and
global in scope? IMHO, anything more than a global
constant would appear extravagant.
p ‘do stuff’ if not $REQUIRED
Reads as ‘not globally required’ and hints that
($REQUIRED == true) would be the ‘normal’ state.
My background support, therefore, for the RCR
(with amendment).
In message “Re: RCR: $INCLUDED global var” > > > on 03/05/16, Joel VanderWerf vjoel@PATH.Berkeley.EDU writes:
What about
MAIN {
# …
}
It’s sorta analogous with BEGIN and END.
Better. But I still want better name.
RUN ?
PROGRAM? (and LIBRARY for the other case, though I can’t think of
anything you’d want in there only if the code was 'require’d).
Actually, I like MAIN - it has a lot of precedent.
Precedent, baggage, whatever My problem with MAIN is that it’s not
a good fit, in the sense that usually what gets put at the bottom is
test code, typically in files that really aren’t designed to be run by
themselves. So the closest equivalent to “main” is probably in
another file (i.e., one that 'require’s this file).
I like the second one, because that’s what I’m usually using the code in
that secion for; unit testing a module or class(s) that appear above in
^^^^^^^^^ What ?
You mean you don’t use the wonderful Test::Unit framework ! Shame !!
Ok, just kidding
Sure I do. Right now I do:
if $0 == FILE
require ‘testunit’
#do unit testing
end
With this suggestion it would change to:
TEST {
require ‘testunit’ #do unit testing
}
I really like your suggestion … I do use it for the same reason. Hope it
gets accepted.
irb(main):001:0> def Foo
irb(main):002:1> puts “hello”
irb(main):003:1> end
=> nil
irb(main):004:0> Foo
NameError: uninitialized constant Foo
from (irb):4
but:
irb(main):005:0> Foo()
hello
=> nil
irb(main):006:0> def Bar
irb(main):007:1> yield
irb(main):008:1> end
=> nil
irb(main):009:0> Bar
NameError: uninitialized constant Bar
from (irb):9
irb(main):010:0> Bar { puts “hello” }
hello
=> nil
I’ll have to file that in the “odd quirks” section of my brain.
Regards,
Brian.
···
On Fri, May 16, 2003 at 08:43:25PM +0900, ts wrote:
If it’s a method then it can’t have an initial capital, can it?