loop puts and appendd
I have the following code that reads mac addresses from a file and tries to append test at the end of the mac address.
File.open("/R开发者_运维技巧ubyDev/sort/mac1.txt",'r').each_line do |a|
puts "#{a} test"
end
This is the output:
SEP1C17D3C23929
test
SEP1C17D3C2B247
test
SEP1C17D3C24B98
test
I want it to be :
SEP1C17D3C23929 test
SEP1C17D3C2B247 test
SEP1C17D3C24B98 test
The problem is that the lines have a new-line ("\n") on the end of them. To get rid of that, you can call String#chomp
:
puts "#{a.chomp} test"
a
is being returned with a newline character in it. You need to do: puts "#{a.strip} test"
精彩评论