得到一個輸入的 char

與 gets.chomp 不同,這不會等待新行。

必須包含 stdlib 的第一部分

require 'io/console'

然後可以編寫一個輔助方法:

def get_char
  input = STDIN.getch
  control_c_code = "\u0003"
  exit(1) if input == control_c_code
  input
end

如果按下 control+c,它就會退出。