Question about 'unless' (negated if)

-----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-----

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

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

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 D.
http://www.rubywizards.com

On Thursday 14 September 2006 01:14 am, Mike D. 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 K.

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

javier ramirez wrote:

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFCYBGKte2c0P8BH0RAlbGAJ4kFr4gM6cvqEBiLEvilw8UscjxEQCghQmC
LD+RrqJDzqL6RqMnHrEQ/Cg=
=veRl
-----END PGP SIGNATURE-----

Randy K. 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.

On 9/14/06, William C. [email protected] wrote:

Randy K. 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.

To me it seems quite natural.

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


Rick DeNatale

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

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

Randy K. wrote:

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

Randy K.

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 A.) 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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFCcNZKte2c0P8BH0RArnPAJ404FGw2tIw6ObdMX/wVbOM8DSR2gCfSqvV
MR+oEW9UYpFOEVc2m7nh9rs=
=QUwh
-----END PGP SIGNATURE-----

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

(I.e., ifnot, somewhat analogous to the elseif construct in some languages).
Just do it.
if not condition

end
Easy :wink:

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 ?)

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

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:

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 K.

On 9/14/06, Randy K. [email protected] 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).

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs