ruby chomp_Ruby中的chomp方法

ruby chomp

Ruby排骨方法 (Ruby chomp method)

chomp is a predefined method in Ruby's library which is used to eliminate implicit newline character '\n' attached with the string entered by the user during run time. We use gets method in Ruby for taking input from the user but it provides a new line with the string. chomp provides help to remove it because newline is not desired in the string most of the times as newline creates problems when some time of processing takes place in the string.

chomp是Ruby库中的预定义方法,用于消除运行时用户输入的字符串附带的隐式换行符'\ n' 。 我们在Ruby中使用gets方法从用户处获取输入,但是它为字符串提供了新的一行。 chomp提供了删除它的帮助,因为大多数情况下,字符串中不需要换行符,因为当在字符串中进行某些时间处理时,换行符会产生问题。

Use of chomp

排骨的使用

For a better understating of chomp, let us understand what happens in the absence of chomp? Read the following code when the chomp is not used and analyze the output.

为了更好地低估排骨 ,让我们了解在没有排骨的情况下会发生什么? 不使用chomp时,请阅读以下代码并分析输出。

puts "Enter your name"
str=gets

puts "Hi "+str+"Welcome to Includehelp."

This might be the most basic program one can write in Ruby. Here we are using gets without chomp method. On the console, when the user will be asked for entering his name, the string will implicitly get a newline character. This will result in the following output,

这可能是人们可以用Ruby编写的最基本的程序。 在这里,我们使用的不带chomp方法的gets 。 在控制台上,当要求用户输入其名称时,该字符串将隐式获得换行符。 这将导致以下输出,

Enter your name
Hitakshi
Hi Hitakshi
Welcome to Includehelp.

So, you can observe in the above output that with the presence of newline, when the string is required to be printed, it creates a gap between whole string by adjusting rest of the string in a new line. No doubt, there is nothing negative in displaying the rest of the characters in a new line but sometimes when the whole string is supposed to be displayed in a single line, it creates a problem.

因此,您可以在上面的输出中观察到,在存在换行符的情况下,当需要打印字符串时,它会通过在新行中调整字符串的其余部分而在整个字符串之间产生间隙。 毫无疑问,在新行中显示其余字符并没有负面影响,但是有时当整个字符串应该显示在一行中时,则会产生问题。

Here, chomp needs to be employed. Let us make changes in the same code by attaching chomp method with the gets method. Observe the code and output after modification.

在这里,需要使用排骨 。 让我们通过将chomp方法附加到gets方法来对同一代码进行更改。 修改后观察代码和输出。

puts "Enter your name"
str=gets.chomp

puts "Hi "+str+" Welcome to Includehelp."

The output after modification is as follows,

修改后的输出如下:

Enter your name
Hitakshi 
Hi Hitakshi Welcome to Includehelp.

You can observe that the above string is getting printed in the same line because now we have made the use of chomp method which is not letting \n to be attached with the string. It is utilized for chopping newline character which is attached at the end of the string.

您可以观察到上面的字符串正在同一行中打印,因为现在我们使用了chomp方法,该方法不允许\ n与该字符串附加。 它用于切碎附加在字符串末尾的换行符。

There is one more method defined in Ruby's method which works like chomp which is called chop but with a basic difference that chomp removes \n from the string whereas ‘chop’ removes any last character from the string.

在Ruby的方法中定义了另一种方法,该方法的工作原理类似于chomp ,称为chop,但基本的区别在于chomp从字符串中删除\ n ,而'chop'从字符串中删除最后一个字符。

翻译自: https://www.includehelp.com/ruby/chomp-method-in-ruby.aspx

ruby chomp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值