Variable empty or not

Hi all,

I am doing some evaluating on a variable,
$Fw_Ip = %x[/usr/bin/plink -pw xxxxxx [email protected] “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.

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs