英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论