Ruby 'gets' 适用于多行 [英] Ruby 'gets' that works over multiple lines

查看:29
本文介绍了Ruby 'gets' 适用于多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 IRB,我想输入一个多行字符串以从中去除某些字符.gets"只允许一行 - 多行是否有类似的功能.

Using the IRB, I want to enter a multiple line string in order to strip certain characters from it. "gets" only allows a single line - is there a similar function for multiple lines.

ASCII_project.rb(main):002:0* puts = "What's the text you want to strip?"
=> "What's the text you want to strip?"
ASCII_project.rb(main):003:0> str = gets

我现在想粘贴一段文本 - 因为新行它不起作用.这就是为什么我想收集多行

I now want to paste in a section of text - because of the new lines it doesn't function. This is why I want to collect over multiple lines

这是代码

# encoding: CP850
puts = "What's the text you want to strip?"
str = gets
str.gsub!(/\P{ASCII}/, '')
puts str

推荐答案

您可以通过以下方式进行,

You can do this in following way,

$/ = "END"  
user_input = STDIN.gets
puts user_input

当您认为输入完成时,请确保输入 END 关键字,

make sure to type END keyword when you think the input is finished,

同样,这只适用于实际的解释器,而不适用于 irb.

As well this will only work with actual interpreter not irb.

这篇关于Ruby 'gets' 适用于多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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