当前位置:  开发笔记 > 编程语言 > 正文

红宝石?如何忽略剪切和粘贴用户输入中的换行符?

如何解决《红宝石?如何忽略剪切和粘贴用户输入中的换行符?》经验,为你挑选了1个好方法。

我写了一些需要一些用户输入的Ruby脚本.我预计在数据输入期间某些时候用户可能会有点懒,需要长条目,并且可能会从包含换行符的其他文档中剪切和粘贴.

我一直在玩Highline宝石,非常喜欢它.我怀疑我只是遗漏了文档中的内容,但有没有办法获得可变长度的多行输入?

编辑:问题是换行符终止了该输入,换行符后的字符最终作为下一个问题的输入.



1> rampion..:

以下是作者在他的例子中使用的内容:(来自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 @key variable is set before each
# question is evaluated, so you can use it in your question.
#
attr_accessor :gather

这些似乎是您在图书馆中的主要选择.别的,你必须自己做.

推荐阅读
黄晓敏3023
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有