rahrahruby
rahrahruby

Reputation: 693

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("/RubyDev/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

Upvotes: 1

Views: 81

Answers (2)

Michael Papile
Michael Papile

Reputation: 6856

a is being returned with a newline character in it. You need to do: puts "#{a.strip} test"

Upvotes: 1

Wayne Conrad
Wayne Conrad

Reputation: 107969

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"

Upvotes: 4

Related Questions