问题描述
我永远不需要从gets
获得的结尾换行符.一半的时间我忘记了chomp
,这对...来说很痛苦.
I never need the ending newline I get from gets
. Half of the time I forget to chomp
it and it is a pain in the....
为什么在那儿?
推荐答案
类似于puts
(听起来很像),它设计为使用\n
字符与线条配合使用.
Like puts
(which sounds similar), it is designed to work with lines, using the \n
character.
gets
带有一个可选参数,用于分割"输入(或一直读取直到到达"),默认为特殊的全局变量$/
,默认情况下包含\n
.
gets
takes an optional argument that is used for "splitting" the input (or "just reading till it arrives). It defaults to the special global variable $/
, which contains a \n
by default.
gets
是用于读取流的非常通用的方法,并包含此分隔符.如果不这样做,部分流内容将丢失.
gets
is a pretty generic method for readings streams and includes this separator. If it would not do it, parts of the stream content would be lost.
这篇关于为何Ruby的"gets"包括结尾的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!