Really stupid primitive Gem that will handle some common console operations.
Don't want to install gem ? There is a copy-paste version for your script Inputs copy-paste version
The gem aims to be really simple. If you looking for something more complex I recommend to use TTY,tty-prompt Reddit discussion
require 'inputs'
Inputs.yn('Are you stupid?')
# Are you stupid? [y/n]
# => true / false
Inputs.name('What is your name')
# What is your name
# => String
Inputs.name!('Do you want to skip this question?')
# What is your name
# => String or Nil if no input
Inputs.password('What is the password: ')
# What is the password:
# => String
Inputs.names('Names of your parents')
# Names of your parents
# => Array
Inputs.pick(['cat', 'dog'])
# Please choose
# Press 1 for "cat"
# Press 2 for "dog"
## after pressing 1
# => 'cat'
## after pressing 2
# => 'dog'
Option 1:
gem install inputs
Option 2 (If you use bundler
) :
# Gemfile
gem 'inputs'
And then execute:
$ bundle
# foo.rb
require 'inputs'
class Foo
def call
name = Inputs.name("what is your name")
if Inputs.yn("Ok #{name}, do you want to learn music instrument?")
instrument = Inputs.pick(instrument_options)
case instrument
when drums
puts 'Metal dude ! \,,/'
when guitar
puts "Rock on dude !"
when harp
puts "W.T.F dude ?"
end
else
puts "Then go home #{name} !"
end
end
private
def instrument_options
[
drums,
guitar,
harp
]
end
def drums
'Drums'
end
def guitar
'Guitar'
end
def harp
'Harp'
end
end
Foo.new.call
The gem is available as open source under the terms of the MIT License.
I welcome any suggestions & Pull Requests. Currently there is not lot of tests as I thought I will never release this lib so if you decide to do a PR please write a test.
Thank you