在Highline的ask方法中随机化echo char?

时间:2014-03-14 13:28:24

标签: ruby highline

我正在尝试将Highline gem的ask方法中的echo字符随机化,但无法使其工作。我没有这样做吗?

srand
ask("password:  ") { |q| q.echo = ('a'.ord+rand(26)).chr }

对于每个ask()调用,字符是随机的,但不是每个字符。第一轮比赛将回应相同的角色,即“cccc'”。下一次运行将回应' mmmm'等等。

1 个答案:

答案 0 :(得分:2)

echo是一个变量值,用于确定是否回显输出。 From the highline source

# [echo] Can be set to +true+ or +false+ to control whether or not input will
# be echoed back to the user.  A setting of +true+ will cause echo to
# match input, but any other true value will be treated as a String to
# echo for each character typed.

您的代码(('a'.ord+rand(26)).chr)每ask评估一次,存储在高线内的echo变量中,然后为输入的每个字符打印出来。

如果不修改换行符,则无法在每个输入字符处打印不同的随机字符。