def with_cwd dir
original_dir = Dir.pwdbeginDir.chdir dir
yieldensureDir.chdir original_dir
endend
December 30th, 2008
2 Comments
Mike
Duncan, you may not have realized that you can simplify this significantly as Dir.chdir already accepts a block:
def with_cwd dir
Dir.chdir dir doyieldendend
It ensures that the current directory only changes for the context of the block. You can dispense with ‘def with_cwd’ and inline it as it is no longer necessary.
December 30th, 2008
duncanbeevers
Thanks Mike,
I should have known when I wanted to do this that Ruby would do the logical thing when handed a block. So nice!
Duncan, you may not have realized that you can simplify this significantly as Dir.chdir already accepts a block:
It ensures that the current directory only changes for the context of the block. You can dispense with ‘def with_cwd’ and inline it as it is no longer necessary.
December 30th, 2008
Thanks Mike,
I should have known when I wanted to do this that Ruby would do the logical thing when handed a block. So nice!
December 31st, 2008