Close

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.

Announcement

I have released my new course on Udemy, Kubernetes By Example. Sign up now to get free lifetime access!

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

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

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