Reputation: 3
My ruby program is causing unwanted line breaks when calling a variable within a string.
The string is then printed to a text file. In this text file there are lot of unwanted line breaks.
Heres my code.
puts 'What is the 2nd Octet?'
second_octet = gets
puts 'What is the 3rd Octet?'
third_octet = gets
puts 'What is the vlan number?'
vlan_number = gets
vrf_number = <<-eos
123#{vlan_number}
eos
router_config = <<-eos
interface Bundle-Ether7.#{vlan_number}
description * #{description_name} *
mtu 9216
vrf #{vrf_number}
ipv4 address 10.#{second_octet}.#{third_octet}.252 255.255.255.0
eos
File.open(config, 'w') { |file| file.write(router_config) }`
I'm getting line breaks after I call the variables so there are gaps between lines, this is extremely annoying with the second_octet and third_octet variables as it splits the ip address across multiple lines.
Any help would be great! Thanks!
Upvotes: 0
Views: 467
Reputation: 5490
The string returned by gets
includes the newline character (\n
or \r\n
) from the return/enter key. You need to do gets.chomp
to take off the trailing newline, or gets.strip
to get rid of leading and trailing whitespace. Either one should work in your case.
Upvotes: 3