# Silly issue with if / else with pingecho

**URL:** <https://www.ruby-forum.com/t/silly-issue-with-if-else-with-pingecho/138976>\
**Category:** Ruby\
**Created:** [May 29, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/silly-issue-with-if-else-with-pingecho/138976 "2008-05-29T17:24:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [May 29, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/silly-issue-with-if-else-with-pingecho/138976/1 "2008-05-29T17:24:42Z")

</div>

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.

What have a messed up?

Thank you.

---

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [May 29, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/silly-issue-with-if-else-with-pingecho/138976/2 "2008-05-29T17:37:57Z")

</div>

Argh… sorry. I just had to chomp the computername.

Argh!

Thanks for reading.

---

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [May 31, 2008, 2:41am UTC](https://www.ruby-forum.com/t/silly-issue-with-if-else-with-pingecho/138976/3 "2008-05-31T02:41:57Z")

</div>

> 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.
> 
> What have a messed up?
> 
> ## Thank you.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Regards,  
Gennady.
