簡體   English   中英

"Ruby:gets.chomp 具有默認值"

[英]Ruby: gets.chomp with 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\"."

你需要做兩件事之一:

1)創建一個gui程序。

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"

使用curses:

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

舊問題的新答案,但添加是因為我發現這是在尋找同一問題的答案。

tty-prompt<\/a>看起來就像您正在尋找的那樣 - 要求輸入具有可編輯的默認值。 這是我發現的唯一可以為我提供可編輯默認值的寶石(但可能還有其他寶石)

完整的代碼如下所示:

require "tty-prompt"
prompt = TTY::Prompt.new
input = prompt.ask('What is your name?', value: 'Bob')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM