为何Ruby的"gets"包括结尾的换行符? [英] Why does Ruby's 'gets' includes the closing newline?

查看:100
本文介绍了为何Ruby的"gets"包括结尾的换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我永远不需要从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"包括结尾的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆