Ruby:带有默认值的gets.chomp

Pet*_*lka 5 ruby bash gets default-value

是否有一些简单的方法如何在提供默认值的同时在 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\"."
Run Code Online (Sandbox Code Playgroud)

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

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

reply = ask_q("Are you hungry?", "Yes")
Run Code Online (Sandbox Code Playgroud)

我知道我可以通过这种方式对 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
Run Code Online (Sandbox Code Playgroud)

……不过好像不是很漂亮。我还需要手动显示默认值,并且用户需要在提示行中重新键入它,如果他想使用它的修改版本(比如yes!代替yes)。

我现在从 Ruby 开始,所以可能有很多语法错误,我也可能遗漏了一些明显的东西......此外,我用谷歌搜索了很多,但令人惊讶的是没有找到任何线索。

TL; DR

为了使问题更清楚,这是您应该在终端中看到的以及我能够在 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
Run Code Online (Sandbox Code Playgroud)

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

### Terminal output of `reply=ask_q("Define name of database." "CURR_DB_NAME")` 
Run Code Online (Sandbox Code Playgroud)

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

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

7st*_*tud 2

您需要执行以下两件事之一:

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

2)使用咒骂。

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

一个图形用户界面程序:

以下是使用 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")
Run Code Online (Sandbox Code Playgroud)

从 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
}
Run Code Online (Sandbox Code Playgroud)

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"
Run Code Online (Sandbox Code Playgroud)

使用诅咒:

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
Run Code Online (Sandbox Code Playgroud)