Can't execute Ruby file using Powershell

↘锁芯ラ 提交于 2019-12-24 01:14:24

问题


I want to execute a ruby file using windows 10 powershell. In my powershell terminal, when I type

ruby test.rb 

I receive

ruby: The term 'ruby' is not recognized as the name of the cmdlet, function, 
script file, or operable program. Check the spelling of the name, or if a 
path was included, verify that the path is correct and try again. 
At line:1 char:1
+ ruby test.rb
    +Category Info        :ObjectNotFound: (ruby:String) [], 
    CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

These commands don't work either

ruby.exe test.rb
rb.exe test.rb

I'm in the right directory. I suspect the problem has to do with PATHs, but I don't understand PATHs yet. I have been trying to execute .rb files in both Powershell and my beginner IDE, Visual Studio Code.

Thanks


回答1:


It is absolutely a problem with your path. Try this:

$Path = 'C:\Path\to\ruby.exe'

& $Path args

Make sure your root folder for ruby.exe is in the $env:Path variable.

$env:Path -split ';'



回答2:


For me I had to kill all instances of powershell -- in my case vscode then relaunch and it picked up the new path environment variables and ruby worked!

Sometimes a full restart is what the doctor prescribed.

I've also had cases where you had to run the program as an administrator to work and have the proper updated new path variable.




回答3:


In powershell, navigate to the directory where your ruby file is stored. It will look something like this C:\Users\Joe\Desktop\Ruby_Practice_folder>

Type

#!/usr/bin/env ruby

Hit enter. Type

ruby your_ruby_file.rb


来源:https://stackoverflow.com/questions/47601063/cant-execute-ruby-file-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!