Skip to content

Instantly share code, notes, and snippets.

@mamantoha
Created October 30, 2012 13:37
Show Gist options
  • Save mamantoha/3980231 to your computer and use it in GitHub Desktop.
Save mamantoha/3980231 to your computer and use it in GitHub Desktop.
Ask for a email and password in Linux terminal
# encoding: utf-8
class AskForCredentials
attr_reader :email, :password
def initialize
ask_for_credentials
end
private
def ask_for_credentials
puts "Enter your credentials."
print "Email: "
@email = ask
print "Password (typing will be hidden): "
@password = ask_for_password
nil
end
def ask
$stdin.gets.to_s.strip
end
def ask_for_password
echo_off
password = ask
puts
echo_on
return password
end
def echo_off
with_tty do
system "stty -echo"
end
end
def echo_on
with_tty do
system "stty echo"
end
end
def with_tty(&block)
return unless $stdin.isatty
yield
end
end
c = AskForCredentials.new
puts c
puts "Email: #{c.email}, Password: #{c.password}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment