Skip to content

Instantly share code, notes, and snippets.

@na0AaooQ
Last active October 16, 2016 03:25
Show Gist options
  • Save na0AaooQ/0001c8e90661c086db649e5cbe8d5556 to your computer and use it in GitHub Desktop.
Save na0AaooQ/0001c8e90661c086db649e5cbe8d5556 to your computer and use it in GitHub Desktop.
Rubyでユーザのキーボード入力を対話的に受け取るRubyスクリプト ref: http://qiita.com/na0AaooQ/items/65005f51c2430f74d2f0
#!/bin/env ruby
## ----------------------------
# ユーザからのキーボードの入力を受け取り、
# yes と入力されたらスクリプトを実行する、no と入力されたらスクリプトを終了します.
## ----------------------------
class ConfirmExecutionClass
def initialize
puts <<-EOT
----------------------------
スクリプトを実行しますか?
スクリプトを実行する場合は yes、実行をキャンセルする場合は no と入力して下さい.
EOT
end
def get_keyboard_input_yes_no
case gets.chomp
when "yes", "YES", "y"
puts " スクリプトを実行します."
when "no", "NO", "n"
puts " スクリプトを終了します."
exit 1
else
puts " yes または no を入力して下さい."
confirm = ConfirmExecutionClass.new
confirm.get_keyboard_input_yes_no
end
end
end
confirm = ConfirmExecutionClass.new()
confirm.get_keyboard_input_yes_no
puts "----------------------------"
puts "hello world!"
[ec2-user@example-ruby-sinatra-server ~]$ which ruby
/usr/local/rbenv/shims/ruby
[ec2-user@example-ruby-sinatra-server ~]$
[ec2-user@example-ruby-sinatra-server ~]$ ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
[ec2-user@example-ruby-sinatra-server ~]$
[ec2-user@example-ruby-sinatra-server ~]$ vi example.rb
[ec2-user@example-ruby-sinatra-server ~]$ ll example.rb
-rw-r--r-- 1 ec2-user ec2-user 1059 10月 16 12:23 example.rb
[ec2-user@example-ruby-sinatra-server ~]$
[ec2-user@example-ruby-sinatra-server ~]$ ruby example.rb
----------------------------
スクリプトを実行しますか?
スクリプトを実行する場合は yes、実行をキャンセルする場合は no と入力して下さい.
yes
スクリプトを実行します.
----------------------------
hello world!
[ec2-user@example-ruby-sinatra-server ~]$
[ec2-user@example-ruby-sinatra-server ~]$ ruby example.rb
----------------------------
スクリプトを実行しますか?
スクリプトを実行する場合は yes、実行をキャンセルする場合は no と入力して下さい.
no
スクリプトを終了します.
[ec2-user@example-ruby-sinatra-server ~]$
[ec2-user@example-ruby-sinatra-server ~]$ ruby example.rb
----------------------------
スクリプトを実行しますか?
スクリプトを実行する場合は yes、実行をキャンセルする場合は no と入力して下さい.
test
yes または no を入力して下さい.
----------------------------
スクリプトを実行しますか?
スクリプトを実行する場合は yes、実行をキャンセルする場合は no と入力して下さい.
yes または no を入力して下さい.
----------------------------
スクリプトを実行しますか?
スクリプトを実行する場合は yes、実行をキャンセルする場合は no と入力して下さい.
yes または no を入力して下さい.
----------------------------
スクリプトを実行しますか?
スクリプトを実行する場合は yes、実行をキャンセルする場合は no と入力して下さい.
no
スクリプトを終了します.
[ec2-user@example-ruby-sinatra-server ~]$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment