如何在 Ruby 中使用“gets”和“gets.chomp”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23193813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:26:58  来源:igfitidea点击:

How to use "gets" and "gets.chomp" in Ruby

ruby

提问by John Wu

I learned that getscreates a new line and asks the user to input something, and gets.chompdoes the same thing except that it does not create a new line. getsmust return an object, so you can call a method on it, right? If so, lets name that object returned by getsas tmp, then you can call the chompmethod of tmp. But before getsreturns tmp, it should print a new line on the screen. So what does chompdo? Does it remove the new line after the getscreated it?

我了解到gets创建一个新行并要求用户输入一些东西,gets.chomp除了它不创建一个新行之外,它还会做同样的事情。gets必须返回一个对象,所以你可以在它上面调用一个方法,对吗?如果是这样,让我们​​将返回的对象命名gets为 as tmp,然后您可以调用 的chomp方法tmp。但是在gets返回之前tmp,它应该在屏幕上打印一个新行。那么有什么作用chomp呢?创建是否删除新行gets

Another way to re-expound my question is: Are the following actions performed when I call gets.chomp?

重新阐述我的问题的另一种方法是:当我调用时是否执行了以下操作gets.chomp

  1. getsprints a new line
  2. getsreturns tmp
  3. tmp.chompremoves the new line
  4. User input
  1. gets打印一个新行
  2. gets返回 tmp
  3. tmp.chomp删除新行
  4. 用户输入

Is this the right order?

这是正确的顺序吗?

回答by Joey

getslets the user input a line and returns it as a value to your program. This value includes the trailing line break. If you then call chompon that value, this line break is cut off. So no, what you have there is incorrect, it should rather be:

gets让用户输入一行并将其作为值返回给您的程序。此值包括尾随换行符。如果您随后调用chomp该值,则此换行符将被切断。所以不,你有什么是不正确的,它应该是:

  1. getsgetsa line of text, includinga line break at the end.
    • This isthe user input
  2. getsreturns that line of text as a string value.
  3. Calling chompon that value removes the line break
  1. gets获取一行文本,包括末尾的换行符。
    • 用户输入
  2. gets将该行文本作为字符串值返回。
  3. 调用chomp该值会删除换行符

The fact that you see the line of text on the screen is only because you entered it there in the first place. getsdoes not magically suppress output of things you entered.

您在屏幕上看到文本行的事实只是因为您首先在那里输入了它。gets不会神奇地抑制您输入的内容的输出。

回答by appostolis

The question shouldn't be "Is this the right order?" but more "is this is the right way of approaching this?"

问题不应该是“这是正确的顺序吗?” 但更多的是“这是解决这个问题的正确方法吗?”

Consider this, which is more or less what you want to achieve:

考虑一下,这或多或少是您想要实现的:

  1. You assign a variable called tmpthe return value of gets, which is a String.
  2. Then you call String's chompmethod on that object and you can see that chompremoved the trailing new-line.

    Actually what chompdoes, is remove the Entercharacter ("\n") at the end of your string. When you type hello, one character at a time, and then press Entergetstakes all the letters andthe Enterkey's new-line character ("\n").

    1. tmp = gets
    hello
    =>"hello\n"
    
    2. tmp.chomp
    "hello"
    
  1. 您分配一个名为tmp的返回值的变量gets,它是一个字符串。
  2. 然后您chomp对该对象调用 String 的方法,您可以看到chomp删除了尾随的换行符。

    实际上chomp是删除字符串末尾的Enter字符(“ \n”)。当您输入hello,一个字符的时间,然后按Entergets需要的所有信件,并Enter关键的换行字符(” \n“)。

    1. tmp = gets
    hello
    =>"hello\n"
    
    2. tmp.chomp
    "hello"
    

getsisyour user's input. Also, it's good to know that *getsmeans "get string" and putsmeans "put string". That means these methods are dealing with Strings only.

gets您用户的输入。此外,很高兴知道 *gets表示“获取字符串”并puts表示“放置字符串”。这意味着这些方法只处理字符串。

回答by mahesh varak

chompis the method to remove trailing new line character i.e. '\n' from the the string. whenever "gets" is use to take i/p from user it appends new line character i.e.'\n' in the end of the string.So to remove '\n' from the string 'chomp' is used.

chomp是从字符串中删除尾随换行符即 '\n' 的方法。每当使用“gets”从用户那里获取 i/p 时,它都会在字符串的末尾附加新行字符 ie'\n'。所以要从字符串中删除 '\n'使用' chomp'。

str = "Hello ruby\n"

str = "你好红宝石\n"

str = str.chomp

str = str.chomp

puts str

把 str

o/p

o/p

"Hello ruby"

“你好红宝石”

回答by RAJESH KAUSHIK

For example:

例如:

x = gets
y = gets
puts x+y

and

x = gets.chomp
y = gets.chomp
puts x+y

Now run the two examples separately and see the difference.

现在分别运行这两个示例并查看差异。

回答by Hassan Jamal

chompreturns a new String with the given record separator removed from the end of str(if present).

chomp返回一个新的字符串,其中给定的记录分隔符从末尾删除str(如果存在)。

See the Ruby String API for more information.

有关更多信息,请参阅Ruby 字符串 API