简体   繁体   English

使用Ruby启动命令提示符不起作用

[英]Start Command Prompt with Ruby doesn't work

I can't get this to work with the Start Command Prompt with Ruby on windows. 我无法通过Windows上的Ruby与Start Command Prompt一起使用。 I got this simple programm: 我得到了这个简单的程序:

puts "Whats your name?"
name = gets
puts "Hello" + name + ". How are you?"

But if I call it with "ruby program.rb", instead for waiting for my input, it just prints out: 但是,如果我用“ ruby​​ program.rb”调用它,而不是等待我的输入,它只会打印出:

Whats your name?
Helloputs "Whats your name?"
. How are you?

It is like the "gets" command is not been recognized. 就像无法识别“ gets”命令。 Why does this happen? 为什么会这样?

It looks like you are (somehow) passing the name of your programm two times on the command line. 看起来您(以某种方式)在命令行上两次传递了程序的名称。 Your described behavior is reproducible when you are running 您运行时所描述的行为是可复制的

ruby program.rb program.rb

This works the way it does since gets does not read from STDIN in all cases. 因为在所有情况下都不会从STDIN读取gets ,所以它的工作方式与之相同。 Instead, it prefers to read the files mentioned on the command line first. 相反,它更喜欢先读取命令行中提到的文件。 Only if there is no additional file on the command line, gets falls back to read from STDIN 仅当命令行上没有其他文件时, gets回退以从STDIN读取

The question on why you are passing the filename of your ruby program twi times is unfortunately less clear. 不幸的是,关于为什么要在两次传递红宝石程序文件名的问题还不清楚。 If you are not calling it that way on your own, this might be caused by some strange environment options in your shell or due to your Ruby setup. 如果您不是以这种方式单独调用它,则可能是由于Shell中某些奇怪的环境选项或Ruby设置引起的。

I was curious as well, and found this link How does gets and gets.chomp in ruby work? 我也很好奇,并且发现了这个链接红宝石中的gets.getmp是如何工作的? Apparently it created a new line therefore could not find the name. 显然,它创建了一个新行,因此找不到该名称。

This seemed to work, (following the instructions in the link) 这似乎可行,(按照链接中的说明进行操作)

puts "Whats your name?"
name = gets
puts "Hello " + name.chomp + ". How are you?"

Have fun. 玩得开心。

Also if you start using rails, you can also test in your console Example 另外,如果您开始使用rails,也可以在控制台中进行测试

> def test1 
>     ...code .. 
> end  

> test1 

@Ray Ban I have used your code @Ray Ban我用过你的代码

puts "Whats your name?"
name = gets
puts "Hello" + name + ". How are you?"

in gets.rb file and run it using $ ruby gets.rb and it worked as expected. 在gets.rb文件中,并使用$ ruby​​ gets.rb运行它,它按预期工作。

I am using ruby-2.3.0 我正在使用ruby-2.3.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM