用Ruby从控制台读取输入?

时间:2020-12-05 20:46:31

I want to write a simple A+B program in ruby, but I have no idea how to work with the console.

我想用ruby编写一个简单的a +B程序,但是我不知道如何使用控制台。

4 个解决方案

#1


192  

Are you talking about gets?

你是在说get吗?

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

内核。获取尝试读取在ARGV中找到的params,并只请求控制台(如果没有找到ARGV)。即使ARGV不是空的,也要强制从控制台读取

#2


27  

you can also pass the parameters through the command line. Command line arguments are stores in the array ARGV. so ARGV[0] is the first number and ARGV[1] the second number

您还可以通过命令行传递参数。命令行参数是数组ARGV中的存储。所以ARGV[0]是第一个数字,ARGV[1]是第二个数字

#!/usr/bin/ruby

first_number = ARGV[0].to_i
second_number = ARGV[1].to_i

puts first_number + second_number

and you call it like this

你这样称呼它

% ./plus.rb 5 6
==> 11

#3


5  

if you want to hold the arguments from Terminal, try the following code:

如果您想保存来自终端的参数,请尝试以下代码:

A = ARGV[0].to_i
B = ARGV[1].to_i

puts "#{A} + #{B} = #{A + B}"

#4


4  

There are many ways to take input from the users. I personally like using the method gets. When you use gets, it gets the string that you typed, and that includes the ENTER key that you pressed to end your input.

有很多方法可以从用户那里获取输入。我个人喜欢用这个方法。当您使用get时,它将获取您输入的字符串,其中包括您按下结束输入的ENTER键。

name = gets
"mukesh\n"

You can see this in irb; type this and you will see the \n, which is the “newline” character that the ENTER key produces: Type name = gets you will see somethings like "mukesh\n" You can get rid of pesky newline character using chomp method.

可以在irb中看到;键入这个,您将看到\n,这是输入键所产生的“换行”字符:键入name =得到您将看到类似“mukesh\n”的东西,您可以使用chomp方法清除烦人的换行字符。

The chomp method gives you back the string, but without the terminating newline. Beautiful chomp method life saviour.

chomp方法返回字符串,但没有终止换行。美丽的咀嚼法拯救生命。

name = gets.chomp
"mukesh"

You can also use terminal to read the input. ARGV is a constant defined in the Object class. It is an instance of the Array class and has access to all the array methods. Since it’s an array, even though it’s a constant, its elements can be modified and cleared with no trouble. By default, Ruby captures all the command line arguments passed to a Ruby program (split by spaces) when the command-line binary is invoked and stores them as strings in the ARGV array.

您还可以使用terminal读取输入。ARGV是对象类中定义的常数。它是数组类的一个实例,可以访问所有数组方法。由于它是一个数组,即使它是一个常量,它的元素也可以毫不费力地进行修改和清除。默认情况下,Ruby捕获在调用命令行二进制文件时传递给Ruby程序的所有命令行参数(按空格分隔),并将它们作为字符串存储在ARGV数组中。

When written inside your Ruby program, ARGV will take take a command line command that looks like this:

当在Ruby程序中编写时,ARGV将接受如下命令行命令:

test.rb hi my name is mukesh

and create an array that looks like this:

创建一个这样的数组:

["hi", "my", "name", "is", "mukesh"]

But, if I want to passed limited input then we can use something like this.

但是,如果我想传递有限的输入那么我们可以使用这样的东西。

test.rb 12 23

and use those input like this in your program:

在你的程序中使用这些输入:

a = ARGV[0]
b = ARGV[1]

#1


192  

Are you talking about gets?

你是在说get吗?

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

内核。获取尝试读取在ARGV中找到的params,并只请求控制台(如果没有找到ARGV)。即使ARGV不是空的,也要强制从控制台读取

#2


27  

you can also pass the parameters through the command line. Command line arguments are stores in the array ARGV. so ARGV[0] is the first number and ARGV[1] the second number

您还可以通过命令行传递参数。命令行参数是数组ARGV中的存储。所以ARGV[0]是第一个数字,ARGV[1]是第二个数字

#!/usr/bin/ruby

first_number = ARGV[0].to_i
second_number = ARGV[1].to_i

puts first_number + second_number

and you call it like this

你这样称呼它

% ./plus.rb 5 6
==> 11

#3


5  

if you want to hold the arguments from Terminal, try the following code:

如果您想保存来自终端的参数,请尝试以下代码:

A = ARGV[0].to_i
B = ARGV[1].to_i

puts "#{A} + #{B} = #{A + B}"

#4


4  

There are many ways to take input from the users. I personally like using the method gets. When you use gets, it gets the string that you typed, and that includes the ENTER key that you pressed to end your input.

有很多方法可以从用户那里获取输入。我个人喜欢用这个方法。当您使用get时,它将获取您输入的字符串,其中包括您按下结束输入的ENTER键。

name = gets
"mukesh\n"

You can see this in irb; type this and you will see the \n, which is the “newline” character that the ENTER key produces: Type name = gets you will see somethings like "mukesh\n" You can get rid of pesky newline character using chomp method.

可以在irb中看到;键入这个,您将看到\n,这是输入键所产生的“换行”字符:键入name =得到您将看到类似“mukesh\n”的东西,您可以使用chomp方法清除烦人的换行字符。

The chomp method gives you back the string, but without the terminating newline. Beautiful chomp method life saviour.

chomp方法返回字符串,但没有终止换行。美丽的咀嚼法拯救生命。

name = gets.chomp
"mukesh"

You can also use terminal to read the input. ARGV is a constant defined in the Object class. It is an instance of the Array class and has access to all the array methods. Since it’s an array, even though it’s a constant, its elements can be modified and cleared with no trouble. By default, Ruby captures all the command line arguments passed to a Ruby program (split by spaces) when the command-line binary is invoked and stores them as strings in the ARGV array.

您还可以使用terminal读取输入。ARGV是对象类中定义的常数。它是数组类的一个实例,可以访问所有数组方法。由于它是一个数组,即使它是一个常量,它的元素也可以毫不费力地进行修改和清除。默认情况下,Ruby捕获在调用命令行二进制文件时传递给Ruby程序的所有命令行参数(按空格分隔),并将它们作为字符串存储在ARGV数组中。

When written inside your Ruby program, ARGV will take take a command line command that looks like this:

当在Ruby程序中编写时,ARGV将接受如下命令行命令:

test.rb hi my name is mukesh

and create an array that looks like this:

创建一个这样的数组:

["hi", "my", "name", "is", "mukesh"]

But, if I want to passed limited input then we can use something like this.

但是,如果我想传递有限的输入那么我们可以使用这样的东西。

test.rb 12 23

and use those input like this in your program:

在你的程序中使用这些输入:

a = ARGV[0]
b = ARGV[1]