From: Heesob Park Date: 2009-03-09T10:19:47+09:00 Subject: Re: I code my first ruby script, and it got problem! 2009/3/9 Power One : > Once again, thank so much Park! > > I wonder why do I have to use ensure to have the subsequent method > del_dir to work? > > After seeing you used ensure, I read on the Internet, it says that > ensure is helping to make sure the code block within ensure will always > be executed. > > Though when I do Dir.chdir("#{@dirname}"), isn't this adequate?  I saw > that you have to use cwd = Dir.pwd, and then do Dir.chdir, and then do > ensure again! > If you did Dir.chdir("#{@dirname}"), the current working directory is "#{@dirname}". So, the next Dir.delete("#{@dirname}") fails to work. > >>   def see_me >>     if File.exist?("#{@dirname}") >>       cwd = Dir.pwd >>       begin >>         Dir.chdir("#{@dirname}") >>         puts Dir.pwd >>       ensure >>         Dir.chdir(cwd) >>       end >>     else >>       puts "This directory #{@dirname} is not exist!" >>     end >>   end >> According to http://www.dweebd.com/ruby/temporarily-change-ruby-working-directory/ The above can be written as def see_me if File.exist?("#{@dirname}") Dir.chdir("#{@dirname}") do puts Dir.pwd end else puts "This directory #{@dirname} is not exist!" end end Regards, Park Heesob