gpt4 book ai didi

Ruby:具有默认值的 gets.chomp

转载 作者:数据小太阳 更新时间:2023-10-29 07:35:57 24 4
gpt4 key购买 nike

有没有一些简单的方法可以在提供默认值的同时要求 Ruby 中的用户输入?

在 bash 中考虑这段代码:

function ask_q {
local PROMPT="$1"
local DEF_V="$2"
read -e -p "$PROMPT" -i "$DEF_V" REPLY
echo $REPLY
}

TEST=$(ask_q "Are you hungry?" "Yes")
echo "Answer was \"$TEST\"."

你能用 Ruby 的 gets.chomp 实现类似的行为吗?

function ask_q(prompt, default="")
puts prompt
reply = gets.chomp() # ???
return reply
def

reply = ask_q("Are you hungry?", "Yes")

我知道我可以通过这种方式对 Ruby 中的功能进行排序......

def ask_q(prompt, default="")
default_msg = (default.to_s.empty?) ? "" : "[default: \"#{default}\"]"
puts "${prompt} ${default}"
reply = gets.chomp()
reply = (default.to_s.empty?) ? default : reply
return reply
end

...不过看起来不是很漂亮。我还需要手动显示默认值,并且用户需要在提示行中重新键入它,如果他想使用它的修改版本(说 yes! 而不是 yes).

我现在开始使用 Ruby,所以可能会出现很多语法错误,而且我也可能会遗漏一些明显的东西......另外,我在 google 上搜索了很多,但令人惊讶的是没有找到任何线索。

TL;博士

为了使问题更清楚,这是您应该在终端中看到的以及我能够在 bash 中实现的(到目前为止还不是在 Ruby 中):

### Terminal output of `reply=ask_q("Are you hungry?" "Yes")`

$ Are you hungry?
$ Yes # default editable value

### Terminal output of `reply=ask_q("What do you want to eat?")`

$ What do you want to eat?
$ # blank line waiting for user input, since there is no second parameter

以及实际情况:我正在为我的网络应用程序构建引导脚本。我需要为用户提供现有的配置数据,他们可以根据需要进行更改。

### Terminal output of `reply=ask_q("Define name of database." "CURR_DB_NAME")` 

我不认为需要切换到 GUI 应用程序世界的花哨功能。

正如我之前所说,这在 bash 中很容易实现。问题是,其他事情是纯粹的痛苦(关联数组,函数没有返回值,传递参数,......)。我想我只需要决定在我的情况下什么最糟糕......

最佳答案

您需要做以下两件事之一:

1) 创建一个图形用户界面程序。

2) 使用诅咒。

就我个人而言,我认为花任何时间学习 curses 都是浪费时间。 Curses 甚至已从 Ruby 标准库中删除。

GUI 程序:

这是使用 Tkinter GUI 框架的 gui 应用程序的样子:

def ask_q(prompt, default="")
require 'tk'

root = TkRoot.new
root.title = "Your Info"

#Display the prompt:
TkLabel.new(root) do
text "#{prompt}: "
pack("side" => "left")
end

#Create a textbox that displays the default value:
results_var = TkVariable.new
results_var.value = default

TkEntry.new(root) do
textvariable results_var
pack("side" => "left")
end

user_input = nil

#Create a button for the user to click to send the input to your program:
TkButton.new(root) do
text "OK"
command(Proc.new do
user_input = results_var.value
root.destroy
end)

pack("side" => "right", "padx"=> "50", "pady"=> "10")
end

Tk.mainloop
user_input
end

puts ask_q("What is your name", "Petr Cibulka")

从 ruby​​ 调用 bash 脚本中的函数:

.../bash_programs/ask_q.sh:

#!/usr/bin/env bash

function ask_q {

local QUESTION="$1"
local DEFAULT_ANSWER="$2"

local PROMPT="$QUESTION"

read -p "$PROMPT $DEFAULT_ANSWER" USERS_ANSWER #I left out the -i stuff, because it doesn't work for my version of bash
echo $USERS_ANSWER
}

ruby_prog.rb:

answer = %x{
source ../bash_programs/ask_q.sh; #When ask_q.sh is not in a directory in your $PATH, this allows the file to be seen.
ask_q 'Are you Hungry?' 'Yes' #Now you can call functions defined inside ask_q.sh
}

p answer.chomp #=> "Maybe"

使用诅咒:

require 'rbcurse/core/util/app'

def help_text
<<-eos
Enter as much help text
here as you want
eos
end

user_answer = "error"

App.new do #Ctrl+Q to terminate curses, or F10(some terminals don't process function keys)
@form.help_manager.help_text = help_text() #User can hit F1 to get help text (some terminals do not process function keys)

question = "Are You Hungry?"
default_answer = "Yes"

row_position = 1
column_position = 10

text_field = Field.new(@form).
name("textfield1").
label(question).
text(default_answer).
display_length(20).
bgcolor(:white).
color(:black).
row(row_position).
col(column_position)

text_field.cursor_end

text_field.bind_key(13, 'return') do
user_answer = text_field.text
throw :close
end

end

puts user_answer

关于Ruby:具有默认值的 gets.chomp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25584864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com