问题
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