I need help in ruby

  • I am creating software, but now I do not want to publish its source code because I do not want people to steal my hard work. I'm not rude and nothing like that. The following is an example of what the program I am creating looks like.
print "Username : " name = gets.chomp print "Password : " pass = gets.chomp if name =="user" and pass=="pa$$word" print "Hello" else print "Error, incorrect details" 

Now this is the simplest form of logging into Ruby, but what happens badly here when the user inserts the wrong information, the program just shuts down, and I want me to want the program to ask the user for the correct information while the right information is being inserted.

  • Are you a Windows user? Know how to program in batch files? examples

    Echo hello world cbs

pause

So here is the code for ruby

 a. print "Command : " b. command = gets.chomp c. if command == "Hello" d. print "Hi, how are you?" e. elsif command == "help" f. print "Say hi to me, chat with me" 

now what i want here is also like the first question

Details: after the user enters "Hello", the program simply shuts down, but I want the program here to ask to send the line again.

-2
source share
3 answers

Here is a simple way: If someone is stuck in a problem that I am facing, that’s all you do. While loop

 number = 1 # Value we will give for a string named "number" while number < 10 # The program will create a loop until the value # of the string named "number" will be greater than 10 puts "hello" # So what we need to do now is to end the loop otherwise # it will continue on forever # So how do we do it? # We will make the ruby run a script that will increase # the string named "number" value every time we run the loop number = number + 1 end # Now what happens is every time we run the code all the program # will do is print "hello" and add number 1 to the string "number". # It will continue to print out "hello" until the string is # greater than 10 
0
source

Use a while that constantly requests input and checks the user's presentation before entering a valid result.

 username = nil while username.nil? puts "What is your username?" entered_username = gets.chomp if entered_username == "Bob" username = entered_username end end puts "Thanks!" 

When launched on the terminal, this produces:

 What is your username? sdf What is your username? dsfsd What is your username? sdfsd What is your username? sdfds What is your username? sdf What is your username? Bob Thanks! 
0
source

1.

 until (print "Username : "; gets.chomp == "user") and (print "Password : "; gets.chomp == "pa$$word") puts "Error, incorrect details" end puts "Hello" 

2.

 loop do print "Command : " case gets.chomp when "Hello" then print "Hi, how are you?"; break when "help" then print "Say hi to me, chat with me"; break end end 
0
source

All Articles