kidoOooOoooOOom

ゲーム開発やってます

rubyのhighlineでパスワード入力を隠すなど

rubyのコンソールアプリでユーザ名とパスワードを入力させる際に、タイプしたパスワードは隠しておきたい場合は highlineを使えばすぐできるとのこと。

require 'highline/import'

username = ask("Enter user: ") { |q| q.echo = true }
password = ask("Enter password: ") { |q| q.echo = false }

これでおk。
highlineの公式を見ると、バリデーションや色づけ表示、選択肢などの機能もあるみたい。

  • バリデーション
ask("retry number?: ", Integer) { |q| q.in = 0..10 }
  • 関数実行
interests = ask("Interests?  (comma sep list)  ", lambda { |str| str.split(/,\s*/) })
  • ANSI color色付けで出力
say("This should be <%= color('bold', BOLD) %>!")
  • 選択肢を手配
choose do |menu|
  menu.prompt = "Please choose your favorite programming language?  "

  menu.choice(:ruby) { say("Good choice!") }
  menu.choices(:python, :perl) { say("Not from around here, are you?") }
end