RCR: $INCLUDED global var

$INCLUDED = (FILE != $0)

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).

martin

Hi,

···

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.

						matz.

Yukihiro Matsumoto wrote:

Hi,

$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.

What about

MAIN {
# …
}

It’s sorta analogous with BEGIN and END.

···

In message “RCR: $INCLUDED global var” > on 03/05/16, Martin DeMello martindemello@yahoo.com writes:

LIB ?

LIBFILE != $0, eg. we have been included.

-a

···

On Fri, 16 May 2003, Yukihiro Matsumoto wrote:

Hi,

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

Hi,

···

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.

What about

MAIN {
# …
}

It’s sorta analogous with BEGIN and END.

And LIB {} (or INCLUDED {}) as the opposite?


Nobu Nakada

Hi,

LIB ?

LIBFILE != $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.

···

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:

What about

MAIN {
# …
}

It’s sorta analogous with BEGIN and END.

Better. But I still want better name.

						matz.

Hi –

···

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:

What about

MAIN {
# …
}

It’s sorta analogous with BEGIN and END.

Better. But I still want better name.

RUN ?

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

“Yukihiro Matsumoto” matz@ruby-lang.org wrote in message news:1053047645.721520.9976.nullmailer@picachu.netlab.jp…

Hi,

LIB ?

LIBFILE != $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


daz

In article 1053047645.721520.9976.nullmailer@picachu.netlab.jp,

Hi,

LIB ?

LIBFILE != $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.

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.

Phil

···

Yukihiro Matsumoto matz@ruby-lang.org wrote:

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:

how about ‘executed?’ ???

if executed?
puts “we are being executed directly :-)”
end

···

On Fri, 16 May 2003 14:14:31 +0900, Yukihiro Matsumoto wrote:

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.


Simon Strandgaard

If it’s a method then it can’t have an initial capital, can it?

if INVOKED_DIRECTLY

end

when_invoked_directly {
# …
}

perhaps??

Regards,

Brian.

···

On Fri, May 16, 2003 at 07:54:36PM +0900, dblack@superlink.net wrote:

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:

What about

MAIN {
# …
}

It’s sorta analogous with BEGIN and END.

Better. But I still want better name.

RUN ?

dblack@superlink.net wrote in message news:Pine.LNX.4.44.0305160656190.25072-100000@candle.superlink.net

Hi –

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.

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:

dblack@superlink.net wrote in message news:Pine.LNX.4.44.0305160656190.25072-100000@candle.superlink.net

Hi –

What about

MAIN {
# …
}

It’s sorta analogous with BEGIN and END.

Better. But I still want better name.

RUN ?

FILEMAIN or FILERUN - to help hint at the equivalence to (FILE == $0)

···

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:

“Phil Tomson” ptkwt@shell1.aracnet.com wrote in message
[snip]

-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
^^^^^^^^^ What ?
You mean you don’t use the wonderful Test::Unit framework ! Shame !!
Ok, just kidding :wink:

I really like your suggestion … I do use it for the same reason. Hope it
gets accepted.

Phil
^^^^^ I like your TaskMaster Framework too :slight_smile:
– 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).

daz

If it's a method then it can't have an initial capital, can it?

svg% ruby -e 'p Integer(12)'
12
svg%

Guy Decoux

Hi –

What about

MAIN {
# …
}

It’s sorta analogous with BEGIN and END.

Better. But I still want better name.

RUN ?

If it’s a method then it can’t have an initial capital, can it?

Yes:

candle:~/hacking/ruby$ cat invoked.rb
def RUN; yield if FILE == $0; end

def talk
puts “Hello!”
end

RUN do
talk
end

candle:~/hacking/ruby$ ruby invoked.rb
Hello!
candle:~/hacking/ruby$ ruby -e ‘require “invoked”’
candle:~/hacking/ruby$

David

···

On Fri, 16 May 2003, Brian Candler wrote:

On Fri, May 16, 2003 at 07:54:36PM +0900, dblack@superlink.net wrote:

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:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi –

···

On Sat, 17 May 2003, Martin DeMello wrote:

dblack@superlink.net wrote in message news:Pine.LNX.4.44.0305160656190.25072-100000@candle.superlink.net

Hi –

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:

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 :slight_smile: 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).

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

In article ba5t77$pdp2p$1@ID-194283.news.dfncis.de,

“Phil Tomson” ptkwt@shell1.aracnet.com wrote in message
[snip]

-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
^^^^^^^^^ What ?
You mean you don’t use the wonderful Test::Unit framework ! Shame !!
Ok, just kidding :wink:

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.

Phil
^^^^^ I like your TaskMaster Framework too :slight_smile:

Thanks.

Phil

···

Shashank Date sdate@everestkc.net wrote:

Hmm. I thought I tried that and it didn’t work.

Ah, it doesn’t work without an argument:

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?

svg% ruby -e ‘p Integer(12)’
12