# Variable empty or not

**URL:** <https://rubytalk.org/t/variable-empty-or-not/67076>\
**Category:** ruby-talk\
**Created:** [16 January 2013 14:19 UTC](https://rubytalk.org/t/variable-empty-or-not/67076 "2013-01-16T14:19:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [16 January 2013 14:19 UTC](https://rubytalk.org/t/variable-empty-or-not/67076/1 "2013-01-16T14:19:14Z")

</div>

Hi all,

I am doing some evaluating on a variable,  
$Fw\_Ip = %x[/usr/bin/plink -pw xxxxxx xxxx@xxxxxx "command" |grep  
INACTIVE]

Now I want to do something depending on if the $Fw\_Ip is empty or not.

if ($Fw\_Ip != nil)  
puts "Active"  
else  
puts "Inactive"  
end

I guess that the variable is not empty so it will always return True but  
if I puts $Fw\_Ip it is empty.

Suggestions on how to solve it would be much appreciated.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Ryan\_Victory](https://avatars.discourse-cdn.com/v4/letter/r/bbe5ce/32.png) [@Ryan\_Victory](https://rubytalk.org/u/Ryan_Victory)\
**Post date:** [16 January 2013 14:46 UTC](https://rubytalk.org/t/variable-empty-or-not/67076/2 "2013-01-16T14:46:02Z")

</div>

nil != ""

Your command will always return something, even if it's just an empty  
string. Replace the check to nil with "" and it should work. Also, two tips:

1. Don't use $variable variable names unless you absolutely need to,  
these are global and make Ruby look like Perl. Also don 't start a  
variable name with a capital letter, this denotes a constant. I'd rename  
your $Fw\_Ip variable to fw\_ip  
2. If you wanted to check for nil, I'd highly recommend the much cleaner  
looking "variable.nil?" syntax, which is also more ruby-esque.

Hope this helps,  
Ryan

> **···**
>
> On 1/16/13 7:19 AM, fox foxmaster wrote:
> 
> > Hi all,
> > 
> > I am doing some evaluating on a variable,  
> > $Fw\_Ip = %x[/usr/bin/plink -pw xxxxxx xxxx@xxxxxx "command" |grep  
> > INACTIVE]
> > 
> > Now I want to do something depending on if the $Fw\_Ip is empty or not.
> > 
> > if ($Fw\_Ip != nil)  
> > puts "Active"  
> > else  
> > puts "Inactive"  
> > end
> > 
> > I guess that the variable is not empty so it will always return True but  
> > if I puts $Fw\_Ip it is empty.
> > 
> > Suggestions on how to solve it would be much appreciated.

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [16 January 2013 15:14 UTC](https://rubytalk.org/t/variable-empty-or-not/67076/3 "2013-01-16T15:14:35Z")

</div>

Thank's Ryan it worked perfectly with "" . I will adjust my code to not  
use global variable since like you say I dont need it, somehow I have  
always used it bad habit I guess.

Thanks once again.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
