简体   繁体   English

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

[英]How to use "gets" and "gets.chomp" in Ruby

I learned that gets creates a new line and asks the user to input something, and gets.chomp does the same thing except that it does not create a new line.我了解到gets创建了一个新行并要求用户输入一些东西, gets.chomp做同样的事情,只是它不创建一个新行。 gets must return an object, so you can call a method on it, right? gets必须返回一个对象,所以你可以在它上面调用一个方法,对吗? If so, lets name that object returned by gets as tmp , then you can call the chomp method of tmp .如果是这样,让我们gets返回的对象命名为tmp ,然后您可以调用tmpchomp方法。 But before gets returns tmp , it should print a new line on the screen.但是在gets返回tmp之前,它应该在屏幕上打印一个新行。 So what does chomp do?那么chomp什么作用呢? Does it remove the new line after the gets created it?gets在创建后删除新行吗?

Another way to re-expound my question is: Are the following actions performed when I call gets.chomp ?另一种重新阐述我的问题的方法是:当我调用gets.chomp时是否执行了以下操作?

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

Is this the right order?这是正确的顺序吗?

gets lets the user input a line and returns it as a value to your program. gets让用户输入一行并将其作为值返回给您的程序。 This value includes the trailing line break.此值包括尾随换行符。 If you then call chomp on that value, this line break is cut off.如果您随后对该值调用chomp ,则此换行符将被切断。 So no, what you have there is incorrect, it should rather be:所以不,你有什么是不正确的,它应该是:

  1. gets gets a line of text, including a line break at the end. gets获取一行文本,包括在最后一个换行符。
    • This is the user input用户输入
  2. gets returns that line of text as a string value. gets将该行文本作为字符串值返回。
  3. Calling chomp on that value removes the line break对该值调用chomp会删除换行符

The fact that you see the line of text on the screen is only because you entered it there in the first place.您在屏幕上看到文本行的事实只是因为您首先在那里输入了它。 gets does not magically suppress output of things you entered. gets不会神奇地抑制您输入的内容的输出。

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 tmp the return value of gets , which is a String.您将一个名为tmp的变量分配给gets的返回值,它是一个字符串。
  2. Then you call String's chomp method on that object and you can see that chomp removed the trailing new-line.然后在该对象上调用 String 的chomp方法,您可以看到chomp删除了尾随的换行符。

    Actually what chomp does, is remove the Enter character (" \\n ") at the end of your string.实际上chomp所做的是删除字符串末尾的Enter字符(“ \\n ”)。 When you type h e l l o , one character at a time, and then press Enter gets takes all the letters and the Enter key's new-line character (" \\n ").当您键入h e l l o 时,一次一个字符,然后按Enter gets获取所有字母Enter键的换行符(“ \\n ”)。

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

gets is your user's input. gets您用户的输入。 Also, it's good to know that * gets means "get string" and puts means "put string".另外,很高兴知道 * gets表示“获取字符串”,而puts表示“放置字符串”。 That means these methods are dealing with Strings only.这意味着这些方法只处理字符串。

chomp is the method to remove trailing new line character ie '\\n' from the the string. chomp是从字符串中删除尾随换行符即 '\\n' 的方法。 whenever "gets" is use to take i/p from user it appends new line character ie'\\n' in the end of the string.So to remove '\\n' from the string ' chomp ' is used.每当使用“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" “你好红宝石”

chomp returns 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

  • "gets" allows user input but a new line will be added after the string (string means text or a sequence of characters) “gets”允许用户输入,但会在字符串后添加一个新行(字符串表示文本或字符序列)

    "gets.chomp" allows user input as well just like "gets", but there is not going to be a new line that is added after the string. “gets.chomp”和“gets”一样也允许用户输入,但不会在字符串后添加新行。

Proof that there are differences between them:证明它们之间存在差异:

Gets.chomp获取.chomp

puts "Enter first text:"
text1 = gets.chomp
puts "Enter second text:"
text2 = gets.chomp
puts text1 + text2

Gets:获取:

puts "Enter first text:"
text1 = gets
puts "Enter second text:"
text2 = gets
puts text1 + text2

Copy paste the code I gave you, run and you will see and know that they are both different.复制粘贴我给你的代码,运行,你会看到并知道它们是不同的。

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.现在分别运行这两个示例并查看差异。

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

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