Home > Software design >  Ruby equivalent of syscall.Exec in golang
Ruby equivalent of syscall.Exec in golang

Time:10-07

I want to set environment variable programatically with ruby. In Golang we have

syscall.Exec(os.Getenv(SHELL), []string{os.Getenv(SHELL)}, updated)

This opens a new default shell with the updated variables. So the terminal where we execute the go program will have those variables persisted for the session.

I'm new to ruby not able to find the equivalent there. Please help me.

CodePudding user response:

To get/set environment variables, you could use ENV hash, then to make system calls, where you could see the standard output (not as ` that will return you the output as a string), you could call system.
The default shell should be available in ENV['SHELL'], so what you'll need should be something like:

ENV['FOO'] = '123' # FOO will last for the entire ruby session
system({'BAR' => '456'}, ENV['SHELL']) # BAR will last until system call has finished
system(ENV['SHELL']) # Here, only FOO will be available, not BAR
  • Related