Ruby equivalent of syscall.Exec in golang

huangapple go评论84阅读模式
英文:

Ruby equivalent of syscall.Exec in golang

问题

我想用Ruby编程来设置环境变量。在Golang中,我们可以使用以下代码来实现:

ENV['SHELL'] = updated
exec(ENV['SHELL'])

这将打开一个新的默认shell,并将更新后的变量传递给它。因此,在执行Ruby程序的终端会话中,这些变量将持久存在。

我是Ruby新手,无法找到等效的方法。请帮助我。

英文:

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.

答案1

得分: 2

要获取/设置环境变量,你可以使用ENV哈希表,然后进行系统调用,其中你可以查看标准输出(不像`那样返回字符串形式的输出),你可以调用system

默认的shell应该可以在ENV['SHELL']中找到,所以你需要的应该是这样的:

ENV['FOO'] = '123' # FOO将在整个Ruby会话中持续存在
system({'BAR' => '456'}, ENV['SHELL']) # BAR将在系统调用完成之前持续存在
system(ENV['SHELL']) # 在这里,只有FOO可用,没有BAR
英文:

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

huangapple
  • 本文由 发表于 2021年10月7日 09:34:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/69474423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定