Module: Hawknee::Helpers
Defined Under Namespace
Classes: BadCommand, BadOption
Instance Method Summary collapse
- #ask(what) ⇒ Object
-
#command_exists? ⇒ Boolean
Used to check if command user passed exists.
-
#confirm?(what = "Are you sure to continue?") ⇒ Boolean
Ask user if he wants to do something.
Instance Method Details
#ask(what) ⇒ Object
4 5 6 7 |
# File 'lib/hawknee/helpers.rb', line 4 def ask(what) print what $stdin.gets.chomp.strip end |
#command_exists? ⇒ Boolean
Used to check if command user passed exists. If not, raises BadCommand, and shows appropriate note.
usage examples:
puts "Good" if command_exists? 'initialize'
28 29 30 31 32 33 34 |
# File 'lib/hawknee/helpers.rb', line 28 def command_exists? begin true if eval "Hawknee::Cli::Command::#{@command.capitalize}.new" rescue NameError raise BadCommand end end |
#confirm?(what = "Are you sure to continue?") ⇒ Boolean
Ask user if he wants to do something. If he passed ānā it exits.
usage examples:
confirm? # Puts "Are you sure to continue? (y/n): ", and waits for input
confirm? "Delete?" # Puts "Delete? (y/n): ", and waits for input
puts "Confirmed" if confirm? # Puts "Confirmed" if user passed 'y'
17 18 19 20 |
# File 'lib/hawknee/helpers.rb', line 17 def confirm?(what = "Are you sure to continue?") print what + " (y/n): " # Hmmm... DRY? Yeah. DRY. We keep ourselves before writing (y/n): several times. if $stdin.gets.chomp.strip =~ /y/i then true else exit end # Exit if input != y end |