From: Stefano Crocco Date: 2008-06-16T00:27:58+09:00 Subject: Re: How !isset in Ruby On Sunday 15 June 2008, Alexey Tafintsev wrote: > Thank you Marc!!!! I rewrite this example :-))) > --------------------- > #!c:/ruby/bin/ruby.exe > > $kcode = "windows-1251" > > require 'cgi' > cgi = CGI.new > > def auth_lang > myauthlang = ENV['HTTP_ACCEPT_LANGUAGE'] > case myauthlang > when /ru/ > alang = "Ошибка 401: Необходима авторизация" > else > alang = "Error 401: Authorization Required" > end > end > > user = ENV['AUTH_USER'] > password = ENV['AUTH_PASSWD'] > > unless defined? user > puts cgi.header({"Status" => "401 Authorization Required", > "Type" => "text/html", "WWW-Authenticate" => "Basic realm=\"Web > Password\""}) > puts auth_lang > else > if user == nil > puts cgi.header({"Status" => "401 Authorization Required", > "Type" => "text/html", "WWW-Authenticate" => "Basic realm=\"Web > Password\""}) > puts auth_lang > else > real_password=password > if real_password!=password > puts cgi.header({"Status" => "401 Authorization Required", > "Type" => "text/html", "WWW-Authenticate" => "Basic realm=\"Web > Password\""}) > puts auth_lang > end > end > end > --------------------- If I'm reading your code correctly, I think your use of defined? is wrong. The user local variable will always be defined, because you assign to it. If ENV doesn't contain the 'AUTH_USER' entry, user will be nil, but it will be defined. If you want to check whether the 'AUTH_USER' environment variable exists, you can use ENV.has_key?('AUTH_USER') or ENV['AUTH_USER'].nil? If you try running your code, you'll see that the branch corresponding to 'AUTH_USER' being undefied is never executed, whether that environment variable exists or not. Stefano