Question about 'unless' (negated if)

Using this snippet:

    dir.each { |str|
        ftp.get(str) unless str.index('d') == 0
        system("mkdir " + lpath + str.split('/').index(str.length-1))
        system("cd " + lpath + str.split('/').index(str.length-1))
        }

Will system be called both times all the time, or will the first one
be called only when the unless condition is satisfied, or will both
calls only be enacted when the unless condition is satisfied? I've
seen examples that used else statements with unless calls, but I'm not
sure if this is needed here or not. Unless is new to me :slight_smile:

Lincoln Anderson wrote:

Will system be called both times all the time, or will the first one
be called only when the unless condition is satisfied, or will both
calls only be enacted when the unless condition is satisfied? I've
seen examples that used else statements with unless calls, but I'm not
sure if this is needed here or not. Unless is new to me :slight_smile:

Just like if:

  unless something
    do_this
    and_this
  end

  do_other unless something_else
  but_always_this

···

--
Posted via http://www.ruby-forum.com/\.

both system calls will be always invoked. The statement under "unless" scope is ftp.get(str), which will be always invoked unless str.index('d') == 0

you can use unless in two ways (shown below). The first one is a one-liner and it goes immediately after the conditioned statement. The second one marks the beginning of the conditioned block. The first one should ring a bell with perl developers and the second with C/Java/Javascript ones

whatever unless condition

unless condition
    whatever
    and_so_on
end

regards,

javier

Lincoln Anderson wrote:

···

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Using this snippet:

    dir.each { |str|
        ftp.get(str) unless str.index('d') == 0
        system("mkdir " + lpath + str.split('/').index(str.length-1))
        system("cd " + lpath + str.split('/').index(str.length-1))
        }

Will system be called both times all the time, or will the first one
be called only when the unless condition is satisfied, or will both
calls only be enacted when the unless condition is satisfied? I've
seen examples that used else statements with unless calls, but I'm not
sure if this is needed here or not. Unless is new to me :slight_smile:
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFFCKITKte2c0P8BH0RAtHAAJkBVZQ5yV8glhpACggeDPcH2RtCxgCeJRPv
PCFuN23jhfTfvcv4NP3rDBY=
=X7eL
-----END PGP SIGNATURE-----

In your example "unless" is applicable only to the line it appears in. Synonym of "unless" is "if not", i.e.:

ftp.get(str) if str.index('d') != 0

Mike Dvorkin
http://www.rubywizards.com

···

On Sep 13, 2006, at 5:28 PM, Lincoln Anderson wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Using this snippet:

    dir.each { |str|
        ftp.get(str) unless str.index('d') == 0
        system("mkdir " + lpath + str.split('/').index(str.length-1))
        system("cd " + lpath + str.split('/').index(str.length-1))
        }

Will system be called both times all the time, or will the first one
be called only when the unless condition is satisfied, or will both
calls only be enacted when the unless condition is satisfied? I've
seen examples that used else statements with unless calls, but I'm not
sure if this is needed here or not. Unless is new to me :slight_smile:
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFCKITKte2c0P8BH0RAtHAAJkBVZQ5yV8glhpACggeDPcH2RtCxgCeJRPv
PCFuN23jhfTfvcv4NP3rDBY=
=X7eL
-----END PGP SIGNATURE-----

Not the OP, but thank you! Somehow "if not" resonates better in my head than
"unless". I wonder if many other people, not yet intimate with unless in
Ruby, find it the same.

Probably too late to change, and I'm not advocating a change, but if I were
writing a new programming language ...

(I.e., ifnot, somewhat analogous to the elseif construct in some languages).

Randy Kramer

···

On Thursday 14 September 2006 01:14 am, Mike Dvorkin wrote:

In your example "unless" is applicable only to the line it appears
in. Synonym of "unless" is "if not", i.e.:

javier ramirez wrote:

both system calls will be always invoked. The statement under
"unless" scope is ftp.get(str), which will be always invoked unless
str.index('d') == 0

you can use unless in two ways (shown below). The first one is a
one-liner and it goes immediately after the conditioned statement.
The second one marks the beginning of the conditioned block. The
first one should ring a bell with perl developers and the second
with C/Java/Javascript ones

whatever unless condition

unless condition whatever and_so_on end

regards,

javier

Thank you for this clarification. I kinda figured this was the case,
but was not sure. Your assistance is greatly appreciated.

Lincoln Anderson

Randy Kramer wrote:

Not the OP, but thank you! Somehow "if not" resonates better in my head
than
"unless". I wonder if many other people, not yet intimate with unless
in
Ruby, find it the same.

Yeah, I usually have to rephrase it in my head still to make 'unless'
flow. I'm still very very new to Ruby though. If I used Ruby more, I'm
sure it would come quicker.

···

--
Posted via http://www.ruby-forum.com/\.

Randy Kramer wrote:

In your example "unless" is applicable only to the line it
appears in. Synonym of "unless" is "if not", i.e.:

Not the OP, but thank you! Somehow "if not" resonates better in my
head than "unless". I wonder if many other people, not yet
intimate with unless in Ruby, find it the same.

Probably too late to change, and I'm not advocating a change, but
if I were writing a new programming language ...

(I.e., ifnot, somewhat analogous to the elseif construct in some
languages).

Randy Kramer

VB has an IsNot operator: If IsNot(a) Then
But I actually like the unless instruction, as it sets an action that
you want to process except in a certain case. The code snippet I
posted is part of a series of FTP functions I'm writing for an
ftpmirror style script. In that case, I want the client to FTP#get
items from a directory listing (obtained through a recursive listing
funtion adapted from a snippet provided by Vincent Arnoux) unless the
item is itself a directory. In that case, I want it mkdir on the
client side a directory of the same name and structure as the one it
encounters.

the revised snippet is:

#untested
def dget(ftp,dir,lpath)
    tpath = lpath
    dir.each {|str|
            ftp.get(str) unless str.index('d') == 0
            else
                str = str.split('/')
                tpath += str[str.length-1]
                system("mkdir " + tpath)
                system("cd " + tpath)
            end
            }
end

I assume this works better? Or in order to use else do I need to
switch to a multiline unless statement:
       unless str.index('d')==0
          ftp.get(str)
       else
          ...
       end

Your continued insight is much appreciated.
Lincoln Anderson

···

On Thursday 14 September 2006 01:14 am, Mike Dvorkin wrote:

Just do it.
if not condition
  ...
end
Easy :wink:

···

On Thu, Sep 14, 2006 at 10:40:16PM +0900, Randy Kramer wrote:

On Thursday 14 September 2006 01:14 am, Mike Dvorkin wrote:
> In your example "unless" is applicable only to the line it appears
> in. Synonym of "unless" is "if not", i.e.:

Not the OP, but thank you! Somehow "if not" resonates better in my head than
"unless". I wonder if many other people, not yet intimate with unless in
Ruby, find it the same.

Probably too late to change, and I'm not advocating a change, but if I were
writing a new programming language ...

(I.e., ifnot, somewhat analogous to the elseif construct in some languages).

For me, it took some years before I started using unless and feel
comfortable with it. However, after I've trained myself to actually
use it, it flows very naturally - more naturally than if not or
"ifnot", as both tokenize in my brain to two tokens, of which one is a
negation - and that's much more work to process than the single case
"unless".

Eivind.

···

On 9/14/06, Randy Kramer <rhkramer@gmail.com> wrote:

Not the OP, but thank you! Somehow "if not" resonates better in my head than
"unless". I wonder if many other people, not yet intimate with unless in
Ruby, find it the same.

Probably too late to change, and I'm not advocating a change, but if I were
writing a new programming language ...

(I.e., ifnot, somewhat analogous to the elseif construct in some languages).

To me it seems quite natural.

    self.play(:golf) unless weather.too_hot?

···

On 9/14/06, William Crawford <wccrawford@gmail.com> wrote:

Randy Kramer wrote:
> Not the OP, but thank you! Somehow "if not" resonates better in my head
> than
> "unless". I wonder if many other people, not yet intimate with unless
> in
> Ruby, find it the same.

Yeah, I usually have to rephrase it in my head still to make 'unless'
flow. I'm still very very new to Ruby though. If I used Ruby more, I'm
sure it would come quicker.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Lincoln Anderson wrote:

    dir.each {|str|
            ftp.get(str) unless str.index('d') == 0
            else
                str = str.split('/')
                # ...
            end
            }
end

You'll get an unexpected kELSE error on line 3. The unless statement is
still applyins only to line 2 here, it acts as the negative form of if
BUT as a oneliner. To actually create a block-like behavior, you must
write something like:

unless 1 == 2
    then puts "foo"
else
    puts "bar"
end

(looks like an if statement, doesn't 'it ?)

I debated about whether to reply to this--there's probably really no need for
a reply, but just to acknowledge that I am listening:

Yes, you are clearly right (in cases where I'm writing code) --I guess I was
more concerned about understanding it when finding it in someone else's
code. :wink:

Randy Kramer

···

On Friday 15 September 2006 09:46 am, Logan Capaldo wrote:

On Thu, Sep 14, 2006 at 10:40:16PM +0900, Randy Kramer wrote:
> On Thursday 14 September 2006 01:14 am, Mike Dvorkin wrote:
> > In your example "unless" is applicable only to the line it appears
> > in. Synonym of "unless" is "if not", i.e.:
>
> Not the OP, but thank you! Somehow "if not" resonates better in my head
> than "unless". I wonder if many other people, not yet intimate with
> unless in Ruby, find it the same.
>
> Probably too late to change, and I'm not advocating a change, but if I
> were writing a new programming language ...
>
> (I.e., ifnot, somewhat analogous to the elseif construct in some
> languages).

Just do it.
if not condition
  ...
end
Easy :wink: