From: Matthew Kerwin Date: 2013-04-24T13:37:35+09:00 Subject: Re: what is the $- magic global? tamouse mailing lists wrote in post #1106716: > On Tue, Apr 23, 2013 at 9:21 PM, Matthew Kerwin > wrote: >> >> I've also encountered perl's @-/@+ match offsets, but can't see how >> they'd be relevant. > > Hummm... I'm flummoxed as well. I just went through the newest > Programming Ruby book (beta 2) and cannot find anything referencing > $-. It lists these: > [snip] > > and this: > > "a global variable name can be formed using $- followed by a single > letter or underscore" [p 318 in PDF] > > but no bare $- variable.... You know what, I think I realise what it is. I was being mislead by the implicit definition of globals. I think I was originally just typing $X-style globals in IRB to see what happened, and in the process I hit $-, thus defining :'$-' as a symbol in the global_variables list. Then whenever I checked global_variables I was seeing $- as a defined (or at least known) symbol. Starting a new ruby instance, I can see that $- isn't defined from the start. irb(main):001:0> $VERBOSE=true => true irb(main):002:0> global_variables.select{|g| g.to_s == '$-' } => [] irb(main):003:0> $- irb(main):004:0> (irb):3: warning: global variable `$-' not initialized => nil irb(main):005:0> global_variables.select{|g| g.to_s == '$-' } => [:$-] irb(main):006:0> Also, having looked a bit closer at parse.y I think I see what's going on: when the parser encounters a '$' followed by a '-' it reads the next character from input (trunk:parse.y#L7839), if it's a identifier character it adds it to the token, otherwise it pushes it back, then it fixes the token, defines the symbol, and returns tGVAR. When I was typing $- into IRB, there was no "next character" so the `nextc()` call was blocking, then when I hit enter again, IRB fed something (a newline or a NUL-byte or EOF or something) to the parser so the blocked parse of the previous line was allowed to complete. If I'm right, this can only happen in IRB, because any other stream will either present a newline/NUL or trigger an EOF, causing nextc() to return. -- Posted via http://www.ruby-forum.com/.