Mark Probert wrote:

> Yes.  The close() in the ensure is then out of scope.  Moving it into the 
> begin .. block like
> 
>   def alive(host, port=80)
>     s = "host=#{host} : "
>     begin
>         t = TCPSocket.new(host, port)
>         s << " made it!"
>         return s
>         t.close
           ^^^^^^^
           This will not be executed.

>     rescue Errno::ETIMEDOUT
>         s << " Timed out -- node unreachable"
>     rescue Exception => e
>         s << " exception = #{e}"
>     end
>   end
> 

begin/rescue/ensure/end are all in the same (enclosing) scope. A 
variable used in 'begin' block is available also in 'rescue' and 
'ensure' blocks as well.

However, in your particular example, as an exception is raised in 'new', 
't' will not reffer to any TCPSocket instance anyways, so doing t.close 
does not make much sense (it will give you runtime error, as 't' will be 
nil in this case).

Gennady.