在Ruby中从控制台读取输入? [英] Read input from console in Ruby?
本文介绍了在Ruby中从控制台读取输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在ruby中编写一个简单的A + B程序,但我不知道如何使用控制台。
解决方案
您在谈论获取
吗?
puts
a = gets.chomp
putsEnter B
b = gets.chomp
c = a.to_i + b.to_i
puts c
<
更新
b
尝试读取 ARGV
中找到的参数,只要求控制台如果不是 ARGV
找到。要强制从控制台读取,即使 ARGV
不为空,请使用 STDIN.gets
I want to write a simple A+B program in ruby, but I have no idea how to work with the console.
解决方案 Are you talking about gets
?
puts "Enter A"
a = gets.chomp
puts "Enter B"
b = gets.chomp
c = a.to_i + b.to_i
puts c
Something like that?
Update
Kernel.gets
tries to read the params found in ARGV
and only asks to console if not ARGV
found. To force to read from console even if ARGV
is not empty use STDIN.gets
这篇关于在Ruby中从控制台读取输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文