Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 可以在带有提示的变量中包含'get.chomp'吗?_Ruby - Fatal编程技术网

Ruby 可以在带有提示的变量中包含'get.chomp'吗?

Ruby 可以在带有提示的变量中包含'get.chomp'吗?,ruby,Ruby,我想知道是否可以在变量中同时包含提示字符和get.chomp。我可以做到: prompt = "> " puts prompt input = gets.chomp 但是有没有一种方法可以将它们都放在一个prompt变量中,这样我在键入put-prompt时就不需要键入input=get.chomp?您可以将它们包装在一个方法中: def ask_for_input prompt = "> " puts prompt gets.chomp end input = a

我想知道是否可以在变量中同时包含提示字符和get.chomp。我可以做到:

prompt = "> "
puts prompt
input = gets.chomp

但是有没有一种方法可以将它们都放在一个
prompt
变量中,这样我在键入
put-prompt
时就不需要键入
input=get.chomp

您可以将它们包装在一个方法中:

def ask_for_input
  prompt = "> "
  puts prompt
  gets.chomp
end


input = ask_for_input # both prints a prompt and reads input

您可以在内核模块中创建自己的
get
方法:

module Kernel
  def my_gets
    gets.chomp
  end
end

input = my_gets

print(input)

我以前总是用高端宝石

require 'highline/import'
name = ask "whats your name"