If I have a string within c1, I can print it in a line by doing:
c1.each_line do |line|
puts line
end
I want to give the number of each line with each line like this:
c1.each_with_index do |line, index|
puts "#{index} #{line}"
end
But that doesn't work on a string.
I tried using $.. When I do that in the above iterator like so:
puts #{$.} #{line}
it prints the line number for the last line on each line.
I also tried using lineno, but that seems to work only when I load a file, and not when I use a string.
How do I print or access the line number for each line on a string?
p File.new(__FILE__).each.with_index { |l,i| puts "line #{i+1}: #{l}" };''. Try it out.