ARGF.each_with_index do |line, idx|
print( "[#{idx}] #{line} x: " );
indent = Indent
# do stuff
indent = ""
end #ARGF e
Each line from STDIN is displayed as,
x: [1] W:\sandbox\tmp\for_each\for_each.rake
Which is not good / odd, if you ask me.
One further unexplained result, the line …
print( "ab: [#{idx}] #{line} x: " );
Displays:
x: ab: [1] W:\sandbox\tmp\for_each\for_each.rake
I just get weird feelings here. Three questions, is this a:
- HOW does the “x:” string wind-up at the beginning of each ‘line‘ the string??!
- Ruby v1.8.7 bug or my bug?
- How to fix it?
Help wanted. I keep looking at this and wonder if I what really dumb thing I did? Many thanks in advance.
aloha, Will
each_linedoes not remove the newlines at the end of the lines, so the strings yielded byeach_linewill contain newlines.printon the other hand will not add newlines to the end of strings (if you want that, useputs). Soprint "hello\nworld:followed byprint "lala", will printwhich explains why your output looks the way it does.
To get the output you want, use
line.chompinstead ofline, which will remove the newline at the end oflineand thus prevent your strings from containing a line break beforex:.