Bug in $.?

wybo>cat t
def out; print "#$. "; end # print $. plus a space
out
$. = 0
out
DATA.each { out }
puts
END
1
2
3
4
wybo>ruby t
7 0 8 9 10 11

This should, probably even without the $.=0 statement, print:
7 0 1 2 3 4

Am I wrong?

Wybo D. wrote:

wybo>cat t
def out; print "#$. "; end # print $. plus a space
out
$. = 0
out
DATA.each { out }
puts
END
1
2
3
4
wybo>ruby t
7 0 8 9 10 11

This should, probably even without the $.=0 statement, print:
7 0 1 2 3 4

Am I wrong?

What looks like is happening (with no opinion as to it’s correctness):

  • DATA.lineno is 7 (6 lines of code + the END line), so the initial
    value’s reasonable

  • when you assign to $. it’s affecting $<.lineno (which makes sense)

  • however when you do “$. = 0”, $< is the handle ARGF, not DATA

  • after the code was read, $< got changed to the ARGF handle but $.
    wasn’t reset

  • you then start reading from DATA, so $. gets associated with it again
    and $. is back to 7

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs