我写了一个需要一些用户输入的小Ruby脚本。我预计用户可能在数据输入期间的某个时间点需要很长时间的输入,并且他们可能从包含换行符的另一个文档中剪切和粘贴。Ruby?如何忽略剪切和粘贴用户输入中的换行符?
我一直在玩Highline宝石,很喜欢它。我怀疑我只是缺少文档中的东西,但有没有办法获得可变长度多行输入?
编辑:问题是换行符终止输入,并且换行符后面的字符最终成为下一个问题的输入。
我写了一个需要一些用户输入的小Ruby脚本。我预计用户可能在数据输入期间的某个时间点需要很长时间的输入,并且他们可能从包含换行符的另一个文档中剪切和粘贴。Ruby?如何忽略剪切和粘贴用户输入中的换行符?
我一直在玩Highline宝石,很喜欢它。我怀疑我只是缺少文档中的东西,但有没有办法获得可变长度多行输入?
编辑:问题是换行符终止输入,并且换行符后面的字符最终成为下一个问题的输入。
下面是笔者在他的例子使用:(从高架-1.5.0 /例)
#!/usr/local/bin/ruby -w
# asking_for_arrays.rb
#
# Created by James Edward Gray II on 2005-07-05.
# Copyright 2005 Gray Productions. All rights reserved.
require "rubygems"
require "highline/import"
require "pp"
grades = ask("Enter test scores (or a blank line to quit):",
lambda { |ans| ans =~ /^-?\d+$/ ? Integer(ans) : ans}) do |q|
q.gather = ""
end
say("Grades:")
pp grades
上HighLine::Question#gather
通用文档(从highline-1.5.0/lib/highline/question.rb)
# When set, the user will be prompted for multiple answers which will
# be collected into an Array or Hash and returned as the final answer.
#
# You can set _gather_ to an Integer to have an Array of exactly that
# many answers collected, or a String/Regexp to match an end input which
# will not be returned in the Array.
#
# Optionally _gather_ can be set to a Hash. In this case, the question
# will be asked once for each key and the answers will be returned in a
# Hash, mapped by key. The <tt>@key</tt> variable is set before each
# question is evaluated, so you can use it in your question.
#
attr_accessor :gather
这些似乎是你的m ain选项w /在图书馆。别的,你必须自己做。
岂不是这样的:
input.gsub!('\r\n', '')
我的理解是否正确:您是否想用几条换行符从命令行捕获用户输入? – aivarsak 2009-03-03 14:33:07
有几个可能的换行符。是。 – srboisvert 2009-03-03 14:35:20
rampion的解决方案效果很好,但仍存在风险,因为只要添加一条空白行就会结束该问题。看看它是否适合你。 – aivarsak 2009-03-03 14:46:26