Ruby – Prompt for user input

In Linux you can prompt a user provide input by using the “read” command. You can do the same thing with Powershell by using the read-host command.

Similarly you can do the same thing in Ruby using the “gets” command:

print "Please enter your name: "
name = gets
print "Welcome #{name.strip}!!!"

Note: I used the strip method to remove any whitespacing.

The above should output the following (assuming you enter “CodingBee” when prompted):

C:\RailsInstaller\Ruby1.9.3\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:/Users/Mir/RailsProjects/spec2xmlproj/helloworld2.rb
Please enter your name: Sher
Welcome Sher!!!
Process finished with exit code 0