You can use the block-version of Dir.chdir
. Inside the block you are in the requested directory, after the Block you are still in the previous directory:
Dir.chdir('mydir'){
%x[#{cmd}]
}
More Related Contents:
- How to call shell commands from Ruby
- Continuously read from STDOUT of external process in Ruby
- How do I test a function with gets.chomp in it?
- What is Ruby’s double-colon `::`?
- How to generate a random string in Ruby
- Code block passed to each works with brackets but not with ‘do’-‘end’ (ruby)
- Array#each vs. Array#map
- Trying to learn / understand Ruby setter and getter methods
- How do I copy a hash in Ruby?
- Is there a “do … while” loop in Ruby?
- what is the functionality of “&: ” operator in ruby? [duplicate]
- Ruby Array Initialization [duplicate]
- What does ‘Monkey Patching’ exactly Mean in Ruby?
- Using ‘return’ in a Ruby block
- Ruby regular expression using variable name
- Best way to convert strings to symbols in hash
- When is `eval` in Ruby justified?
- How can I install a local gem?
- How to convert a string or integer to binary in Ruby?
- What does to_proc method mean in Ruby?
- When to use ‘self’ in Ruby
- What is a regex to match a string NOT at the end of a line?
- Can Ruby print out time difference (duration) readily?
- How do I format a date in ruby to include “rd” as in “3rd”
- What is the difference between Raising Exceptions vs Throwing Exceptions in Ruby?
- List of installed gems?
- What are those pipe symbols for in Ruby?
- -bash: __git_ps1: command not found
- Ruby’s yield feature in relation to computer science
- Why isn’t current directory on my Ruby path? [duplicate]