2

ユーザー入力が必要な小さな Ruby スクリプトを作成しました。ユーザーは、長いエントリが必要なデータ入力中のある時点で少し怠け者になり、改行を含む別のドキュメントからカット アンド ペーストする可能性があると予想されます。

私はハイラインの宝石で遊んでいて、とても気に入っています。ドキュメントに何かが欠けているのではないかと思いますが、可変長の複数行入力を取得する方法はありますか?

編集:問題は、改行がその入力を終了し、改行の後の文字が次の質問の入力として終了することです。

4

2 に答える 2

5

著者が彼の例で使用しているものは次のとおりです:(highline-1.5.0 / examplesから)

#!/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

これらは、ライブラリでの主なオプションのようです。それ以外は、自分でやらなければなりません。

于 2009-03-03T14:32:34.220 に答える
0

それは次のようなものではないでしょうか:

input.gsub!('\r\n', '')
于 2009-03-03T12:35:49.337 に答える