Unless unless

Discussion in 'Ruby' started by Gábor SEBESTYÉN, Jun 17, 2005.

  1. I have this very simple example:

    a =3D 1
    b =3D nil
    puts a.nil?
    puts b.nil?
    puts "ok" unless a.nil? and b.nil?

    Running it results these:

    false
    true
    ok

    Why the third answer is ok? Unless false and true =3D> false ..
    Clue?

    G=E1bor
     
    Gábor SEBESTYÉN, Jun 17, 2005
    #1
    1. Advertising

  2. Gábor SEBESTYÉN

    Renald Buter Guest

    On 17:44 Fri 17 Jun , G?bor SEBESTY?N wrote:
    > I have this very simple example:
    >
    > a = 1
    > b = nil
    > puts a.nil?
    > puts b.nil?
    > puts "ok" unless a.nil? and b.nil?
    >
    > Running it results these:
    >
    > false
    > true
    > ok
    >
    > Why the third answer is ok? Unless false and true => false ..


    Just look carefully and mind that 'unless' is equivalent to 'if not'
    a.nil? => false
    b.nil? => true
    false and true => false
    if not false => if true

    Regards,

    Renald
     
    Renald Buter, Jun 17, 2005
    #2
    1. Advertising

  3. Gábor SEBESTYÉN wrote:
    > I have this very simple example:
    >
    > a = 1
    > b = nil
    > puts a.nil?
    > puts b.nil?
    > puts "ok" unless a.nil? and b.nil?
    >
    > Running it results these:
    >
    > false
    > true
    > ok
    >
    > Why the third answer is ok? Unless false and true => false ..
    > Clue?


    That's standard logic. Maybe this helps:

    >> a = 1

    => 1
    >> b = nil

    => nil
    >> puts a.nil?

    false
    => nil
    >> puts b.nil?

    true
    => nil
    >> puts "ok" unless a.nil? and b.nil?

    ok
    => nil
    >> a.nil? and b.nil?

    => false
    >> puts "ok" unless false

    ok
    => nil
    >> puts "ok" if not false

    ok
    => nil

    Kind regards

    robert
     
    Robert Klemme, Jun 17, 2005
    #3
  4. --Apple-Mail-1--1042675108
    Content-Transfer-Encoding: quoted-printable
    Content-Type: text/plain;
    charset=ISO-8859-1;
    format=flowed


    On 2005.06.17., at 10:48, Renald Buter wrote:

    > Just look carefully and mind that 'unless' is equivalent to 'if not'
    >

    Eee .. thanks ... need more coffee! :))

    G=E1bor=

    --Apple-Mail-1--1042675108--
     
    Gábor SEBESTYÉN, Jun 17, 2005
    #4
    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. James Dyer
    Replies:
    5
    Views:
    688
  2. Bennett Haselton
    Replies:
    5
    Views:
    611
    Bennett Haselton
    Aug 19, 2003
  3. Anders Borum
    Replies:
    0
    Views:
    3,504
    Anders Borum
    Jun 9, 2004
  4. Don
    Replies:
    3
    Views:
    928
    Teemu Keiski
    Jul 9, 2004
  5. WTH
    Replies:
    1
    Views:
    367
    Rick Strahl [MVP]
    Nov 2, 2004
Loading...

Share This Page