开发者

loop puts and appendd

开发者 https://www.devze.com 2023-02-12 13:52 出处:网络
I have the following code that reads mac addresses from a file and tries to append test at the end of the mac address.

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"

0

精彩评论

暂无评论...
验证码 换一张
取 消