I’m positive that this is something incredibly silly. I have a text file
with computer names. I’m assuming that if a computer returns a ping then
it exists. To check this I have the following in a script:
def computer_exists?(computername)
Ping.pingecho(computername)
end
file = File.new(FILENAME, "r")
while (line_item = file.gets)
line_item = line_item.gsub(/\s+$/, $/) #remove trailing white
space
puts "Working on #{line_item}"
if computer_exists?(line_item)
deploy(line_item)
else
puts "Timed out"
end
end
file.close
The file contains three computer items:
ComputerA
ComputerB
ComputerC
When I go to irb and I do Ping.pingecho(‘ComputerA’) it turns it as
true. All three computer exist and are pingable. However, when I run the
above code, ruby outputs Computer A and Computer B as Timed out but
Computer C runs the deploy method.
If I add a ComputerD, then ComputerA,B,C say time out and only Computer
D runs the deploy method.
it exists. To check this I have the following in a script:
def computer_exists?(computername)
Ping.pingecho(computername)
end
file = File.new(FILENAME, "r")
while (line_item = file.gets)
line_item = line_item.gsub(/\s+$/, $/) #remove trailing white
Here you do not remove trailing whitespace, but rather replace it with a
single “\n” (default value of $/). This will work as you wanted:
line_item = line_item.gsub(/\s+$/, “”) #remove trailing white
Or as you correctly concluded in your follow-up reply (I think), same in
your case can be achieved with ‘chomp’:
line_item = line_item.chomp
However, I would use ‘strip’ to remove both leading and trailing
whitespace, just to be a little bit more friendly with the file format:
line_item = line_item.strip
end
above code, ruby outputs Computer A and Computer B as Timed out but
Computer C runs the deploy method.
If I add a ComputerD, then ComputerA,B,C say time out and only Computer
D runs the deploy method.