To Change Directory Inside a Ruby Script

To change directory inside a ruby script?

Use Dir.chdir:

Dir.chdir "my_app"

Ruby: change directory

Write as below :

Dir.chdir(Dir.pwd+"/app/assets")

Dir::pwd Returns the path to the current working directory of this process as a string. Then Dir::chdir Changes the current working directory of the process to the given string. Remember if you are already in C:/Sites/todo, then only Dir.chdir("app/assets") will work.

One example to illustrate this :

irb(main):001:0> Dir.chdir('C:\Users\rakshiar')
=> 0
irb(main):002:0> Dir.pwd
=> "C:/Users/rakshiar"
irb(main):003:0> Dir.chdir('userdata\Tax form Demo')
=> 0
irb(main):004:0> Dir.pwd
=> "C:/Users/rakshiar/userdata/Tax form Demo"
irb(main):005:0>

Ruby change directory in code

The current working directory is always kept for the current program only. Changing the working directory in a program won't affect any other running programs, including its parent.

Thus, when you delete the directory in your Ruby script and change the working directory of the Ruby process one level down, this won't affect the shell process which has started your Ruby script.

Change Directory with Ruby (with side effects?)


No, it is not possible.

In fact, no child process can change the current working directory of its parent process.

When you execute a script (or any program) from your command shell you are actually doing a "fork/exec" pair, which means you create a "child process" which is separate from your shell "parent process" in many ways. The child can make changes to its own environment but cannot (typically) change the parent environment.

change directory with cmd bash into ruby

The solution is in this code.

def pr_cartella
@cartella=ARGV[0] #prendo l'argomento
Dir.mkdir @cartella #creo la cartella dall'argomento ottenuto
if Dir.exist? @cartella
puts "bene la cartella è stata creata"
Dir.chdir @cartella
exec 'bash'
end
end
pr_cartella

thanks to all.

exec the cd command in a ruby script

As other answers already pointed out, you can change the pwd inside your ruby script, but it only affects the child process (your ruby script). A parent process' pwd cannot be changed from a child process.

One workaround could be, to have the script output the shell command(s) to execute and call it in backticks (i.e. the shell executes the script and takes its output as commands to execute)

myscript.rb:

# … fancy code to build somepath …
puts "cd #{somepath}"

to call it:

`ruby myscript.rb`

make it a simple command by using an alias:

alias myscript='`ruby /path/to/myscript.rb`'

Unfortunately, this way you can't have your script output text to the user since any text the script outputs is executed by the shell (though there are more workarounds to this as well).

How do I run commands in a given directory in Ruby?

try: Dir.chdir method. Reference: http://www.ruby-doc.org/core-2.1.0/Dir.html#method-c-chdir

Dir.chdir(directory) do
run("bower install", capture: true, verbose: false)
end

The above example code will change to the directory represented by the directory variable, and then run whatever commands you pass to this block.



Related Topics



Leave a reply



Submit