Bug #3022

What are $. and ARGF.lineno ?

Added by Marc-Andre Lafortune over 5 years ago. Updated almost 3 years ago.

[ruby-core:29048]
Status:Closed
Priority:Normal
Assignee:Eric Hodel
ruby -v:ruby 1.9.2dev (2010-03-03 trunk 26805) [x86_64-darwin10.2.0] Backport:

Description

=begin
1) $. is not officially documented.

This makes it difficult to know if it works as expected or not...

The Ruby Prog Language (Flanagan & Matz) states:
"The number of the last line read from the current input file. Equivalent to ARGF.lineno."

This is not true in trunk, as demonstrated by:

$ rubydev -e "
ARGF.gets
File.open('/etc/passwd'){|f| f.gets; f.gets}
p $., ARGF.lineno
" /etc/hosts
2
1

What is the "current input file"? Not clear, but it's not thread local, as shown:

$ rubydev -e "
p $.
Thread.new{File.open('/etc/passwd').gets; p $. }.join;
p $.
"
0
1
1

2) ARGF.lineno does not conform to its doc.

The doc states:

Returns the current line number of the current file in ARGF. This value
can be set manually with ARGF.lineno=.

Reading this, I would expect ARGF.lineno to be the same as ARGF.to_io.lineno.

That is not the case:

rubydev -e 'p "#{ARGF.lineno} #{ARGF.to_io.lineno}" while ARGF.gets' /etc/hosts /etc/passwd
"1 1"
"2 2"
...
"25 1"
"26 2"
...

1) Maybe the best definition would be that $. returns the number of line read operations issued, from the last time an IO was read in the current thread?

2) I suggest the documentation of ARGF.lineno be changed to:

Returns the current line number of ARGF as a whole. This value
can be set manually with ARGF.lineno=.

See also
=end

Associated revisions

Revision 37913
Added by Zachary Scott almost 3 years ago

  • doc/globals.rdoc: Add documentation file for magic globals [Bug #3022]

Revision 37913
Added by Zachary Scott almost 3 years ago

  • doc/globals.rdoc: Add documentation file for magic globals [Bug #3022]

History

#1 Updated by Yusuke Endoh over 5 years ago

  • Assignee set to Marc-Andre Lafortune

=begin
Hi,

2010/3/27 Marc-Andre Lafortune redmine@ruby-lang.org:

This is not true in trunk, as demonstrated by:

$ rubydev -e "
ARGF.gets
File.open('/etc/passwd'){|f| f.gets; f.gets}
p $., ARGF.lineno
" /etc/hosts
2
1

This can be fixed easily, but I guess it will cause compatibility
issue. In fact, by the fix, rubyspec and make test-all reports
some errors. So I think we should consider the current behavior
as spec:

Returns the number of the last line read by Kernel#gets, IO#gets
or ARGF#gets. This variable has global scope, and can be assigned
an integer manually.

2) I suggest the documentation of ARGF.lineno be changed to:

Returns the current line number of ARGF as a whole. This value
can be set manually with ARGF.lineno=.

Looks good. Could you commit it with the doc I wrote together?

--
Yusuke Endoh mame@tsg.ne.jp
=end

#2 Updated by Marc-Andre Lafortune about 5 years ago

  • % Done changed from 0 to 100
  • Status changed from Open to Closed

=begin
This issue was solved with changeset r28381.
Marc-Andre, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

=end

#3 Updated by Marc-Andre Lafortune about 5 years ago

  • Status changed from Closed to Open

=begin
Writing "cf." in the commit log is not recognized correctly, but "cf", "c.f." or "c.f" are.

Since "cf." is the correct way to write it, could anyone with the right access fix it (Yugui?)

Thanks, and sorry for the noise
--
Marc-André

=end

#4 Updated by Yusuke Endoh about 5 years ago

  • Target version changed from 1.9.2 to 2.0.0
  • Assignee deleted (Marc-Andre Lafortune)
  • Priority changed from Normal to 3

=begin
Hi,

1) $. is not officially documented.

As far as I know, rdoc does not support documents for built-in
variables. So there is no way to document them.

We should first add the feature to rdoc, or add a new document
about ruby's basic semantics. Contribution is welcome.

--
Yusuke Endoh mame@tsg.ne.jp
=end

#5 Updated by Yui NARUSE about 4 years ago

  • Status changed from Open to Assigned
  • Assignee set to Eric Hodel

Eric, how do we document $.?

#6 Updated by Steve Klabnik about 4 years ago

Perl actually keeps a separate document to explain these variables, 'Perlvar' http://perldoc.perl.org/perlvar.html

#7 Updated by Eric Hodel about 4 years ago

  • Category changed from core to doc

I will need to add support to RDoc for global variables, I don't have time to add the feature for 1.9.3.

ARGF.lineno is documented now.

#8 Updated by Zachary Scott almost 3 years ago

  • Status changed from Assigned to Closed

This issue was solved with changeset r37913.
Marc-Andre, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • doc/globals.rdoc: Add documentation file for magic globals [Bug #3022]

Also available in: Atom PDF