When processing a file, I used to use the special variable $.
to get the last line number being read. For instance, the following program
require 'csv'
IFS=';'
CSV_OPTIONS = { col_sep: IFS, external_encoding: Encoding::ISO_8859_1, internal_encoding: Encoding::UTF_8 }
CSV.new($stdin, CSV_OPTIONS).each do |row|
puts "::::line #{$.} row=#{row}"
end
is supposed to dump a CSV file (where the fields are delimited by semicolon instead of comma, as is the case in our project) and prepend each output line by the line number.
After updating Ruby to
_ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-cygwin]_
the lines are still dumped, but the line number is always displayed as zero.
What strikes me, is that this Ruby Wiki on special Ruby variables, while still having $.
in its list, doesn't have a description for this variable anymore. So I wonder: Is this variable gone, or was it never supposed to work with the csv class and just worked for me by accident in the earlier versions?
$.
predefined global variable which stores the current input line number of the last file that was read. That means the probably the implementation ofCSV
change. – spickermann Jun 10 '19 at 10:59CSV.new($stdin, CSV_OPTIONS).each_with_index do |row, line|
? Why there is a need in a global variable in the first place? – Aleksei Matiushkin Jun 10 '19 at 11:13each
andeach_with_index
behaviour. – Aleksei Matiushkin Jun 10 '19 at 11:32each.with_index
then. I am not aware of what CSV provides in particular, buteach
returns anEnumerator
instance when no block is given. – Aleksei Matiushkin Jun 10 '19 at 11:43each_with_index
will not cause the whole file to be read into memory at once. – Jordan Running Jun 10 '19 at 17:14