Forum: Ruby Variable empty or not

Posted by fox foxmaster (foxmaster)
on 2013-01-16 15:19
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 by Ryan Victory (Guest)
on 2013-01-16 15:46
(Received via mailing list)
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
Posted by fox foxmaster (foxmaster)
on 2013-01-16 16:14
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.