From: Daniel Schierbeck Date: 2006-08-15T21:10:09+09:00 Subject: Re: question about if-statements fabsy wrote: > Im a newbie to ruby (hehe). > I have a question. > Is there any better way to do this? : > > --------------- > puts "Enter Username" > usr = gets > puts "Enter Password" > usr_pass = gets > User = "user" > Pass = "pass" > > if usr > User > if usr_pass > Pass > puts "Correct user and pass" > else > puts "Correct user" > puts "Wrong pass" > end > else > if usr_pass > Pass > puts "Correct pass" > else > puts "Wrong pass" > end > puts "Wrong user" > end > > ------------------------This is the same but in VB--------- > if usr = User and usr_pass = Pass then > print "correct user and password" > else > if usr = User and usr_pass <> Pass then > print "correct user and wrong password" > else > if usr <> User and usr_pass = Pass then > print "correct password and wrong user" > else > if usr <> User and usr_pass <> Pass then > print "wrong password and wrong user" > end if > end if > end if > end if > ----------------------------------------------------- > > -------And how do i do this in ruby? ------ > If usr = User and usr_pass = Pass then > print "Correct User and Pass" > else > print "Wrong User or Pass" > endif Question 1: print "enter username: " username = gets.chomp # chomp removes the last newline print "enter password: " password = gets.chomp # using constants may not be the best approach... Username = "foo" Password = "bar" raise "incorrect username" unless username == Username raise "incorrect password" unless password == Password # the user is now authenticated Question 2: if username == Username and password == Password # correct else # incorrect end Cheers, Daniel