question about if-statements

Discussion in 'Ruby' started by fabsy, Aug 15, 2006.

  1. fabsy

    fabsy Guest

    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
     
    fabsy, Aug 15, 2006
    #1
    1. Advertising

  2. 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
     
    Daniel Schierbeck, Aug 15, 2006
    #2
    1. Advertising

  3. On 15.08.2006 13:44, 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


    puts "Enter Username"
    usr = gets
    puts "Enter Password"
    usr_pass = gets

    if usr > "user" && usr_pass > "pass"
    puts "Login ok"
    else
    puts "Wrong credentials"
    end

    Note: typically you do not report whether the user name or the password
    was invalid to give attackers as few information as possible.

    Btw, why do you compare with greater than and a string?

    Kind regards

    robert
     
    Robert Klemme, Aug 15, 2006
    #3
  4. fabsy

    fabsy Guest

    Thanks!

    > Btw, why do you compare with greater than and a string?

    I didn't know how to compare so I just guessed it would be greater
    than.
    How would you do if you where to do a program like mine? The way you
    wrote or any other way?
     
    fabsy, Aug 15, 2006
    #4
  5. On Aug 15, 2006, at 14:40, fabsy wrote:

    > Thanks!
    >
    >> Btw, why do you compare with greater than and a string?

    > I didn't know how to compare so I just guessed it would be greater
    > than.
    > How would you do if you where to do a program like mine? The way you
    > wrote or any other way?


    Well, you're testing for equality, so greater than wouldn't catch a
    lot of cases. I'd suggest these:

    if X != Y # X not equal to Y

    unless X == Y # X equal to Y

    You can pick the one that's more linguistically appealing to you,
    though I have a hunch that 'if' is more popular than 'unless'.

    matthew smillie.
     
    Matthew Smillie, Aug 15, 2006
    #5
  6. Matthew Smillie wrote:
    > if X != Y # X not equal to Y
    >
    > unless X == Y # X equal to Y
    >
    > You can pick the one that's more linguistically appealing to you, though
    > I have a hunch that 'if' is more popular than 'unless'.


    I only think that's because most other languages don't have `unless' :)


    Cheers,
    Daniel
     
    Daniel Schierbeck, Aug 15, 2006
    #6
  7. On Aug 15, 2006, at 15:05, Daniel Schierbeck wrote:

    > Matthew Smillie wrote:
    >> if X != Y # X not equal to Y
    >> unless X == Y # X equal to Y
    >> You can pick the one that's more linguistically appealing to you,
    >> though I have a hunch that 'if' is more popular than 'unless'.

    >
    > I only think that's because most other languages don't have
    > `unless' :)


    That was my first intuition too, and I'm certain that's part of it,
    but upon further reflection I think there's more to it as well. To
    make an if and unless statement equivalent, you have to negate the
    condition, leading to this basic schema:

    (1) if X != Y <--> unless X == Y
    (2) if X == Y <--> unless X != Y

    Logically, everything's kosher, but linguistically there's a crucial
    difference: the 'unless' form of (2) is a double negative. I'm sure
    people are generally familiar with the admonition to avoid double
    negatives in their writing, and it's for a good reason: people have a
    hard time understanding multiple negations; to be fair, two is
    usually not a problem, especially in familiar forms such as "not
    unlike X", but in general it's not an easy task to not do
    incorrectly. (see?)

    So, if you assume that given the choice people won't use
    linguistically-uncomfortable code, then there are two basic
    comfortable 'if' forms, but only one comfortable 'unless' form.
    Given the lovely, literary nature of Ruby code, this seems like a
    reasonable assumption to make; so even if everyone were perfectly
    familiar with 'unless' as a language construct, you'd still expect
    'if' to outnumber 'unless'.

    Not that I think this has much bearing on the language, just a neat
    observation.

    matthew smillie

    [1] For an entertaining example, see the Language Log talking about a
    Penny Arcade comic strip here:
    http://itre.cis.upenn.edu/~myl/languagelog/archives/003437.html
     
    Matthew Smillie, Aug 15, 2006
    #7
  8. Matthew Smillie wrote:
    > On Aug 15, 2006, at 15:05, Daniel Schierbeck wrote:
    >
    >> Matthew Smillie wrote:
    >>> if X != Y # X not equal to Y
    >>> unless X == Y # X equal to Y
    >>> You can pick the one that's more linguistically appealing to you,
    >>> though I have a hunch that 'if' is more popular than 'unless'.

    >>
    >> I only think that's because most other languages don't have `unless' :)

    >
    > That was my first intuition too, and I'm certain that's part of it, but
    > upon further reflection I think there's more to it as well. To make an
    > if and unless statement equivalent, you have to negate the condition,
    > leading to this basic schema:
    >
    > (1) if X != Y <--> unless X == Y
    > (2) if X == Y <--> unless X != Y
    >
    > Logically, everything's kosher, but linguistically there's a crucial
    > difference: the 'unless' form of (2) is a double negative. I'm sure
    > people are generally familiar with the admonition to avoid double
    > negatives in their writing, and it's for a good reason: people have a
    > hard time understanding multiple negations; to be fair, two is usually
    > not a problem, especially in familiar forms such as "not unlike X", but
    > in general it's not an easy task to not do incorrectly. (see?)
    >
    > So, if you assume that given the choice people won't use
    > linguistically-uncomfortable code, then there are two basic comfortable
    > 'if' forms, but only one comfortable 'unless' form. Given the lovely,
    > literary nature of Ruby code, this seems like a reasonable assumption to
    > make; so even if everyone were perfectly familiar with 'unless' as a
    > language construct, you'd still expect 'if' to outnumber 'unless'.
    >
    > Not that I think this has much bearing on the language, just a neat
    > observation.


    Interesting thoughts (and cool example). I do however tend to use short,
    one-liner conditional statements the most, in which I think `unless'
    fits much nicer than `if not'.


    Cheers,
    Daniel
     
    Daniel Schierbeck, Aug 15, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    8
    Views:
    1,196
    mfmehdi
    Oct 20, 2006
  2. thetwistedpanda

    Question on if/else statements

    thetwistedpanda, Aug 20, 2004, in forum: C++
    Replies:
    5
    Views:
    418
    Thomas Matthews
    Aug 23, 2004
  3. Harry George
    Replies:
    6
    Views:
    384
    Bart Nessux
    Feb 23, 2004
  4. Vince
    Replies:
    12
    Views:
    751
    Martin Gregorie
    Jan 21, 2008
  5. John Crichton
    Replies:
    6
    Views:
    267
    John Crichton
    Jul 12, 2010
Loading...

Share This Page