[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Question about 'unless' (negated if

Lincoln Anderson

9/14/2006 12:29:00 AM

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

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


12 Answers

e

9/14/2006 1:05:00 AM

0

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

Just like if:

unless something
do_this
and_this
end

do_other unless something_else
but_always_this


--
Posted via http://www.ruby-....

javier ramirez

9/14/2006 1:21:00 AM

0

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 :)
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail....
>
> iD8DBQFFCKITKte2c0P8BH0RAtHAAJkBVZQ5yV8glhpACggeDPcH2RtCxgCeJRPv
> PCFuN23jhfTfvcv4NP3rDBY=
> =X7eL
> -----END PGP SIGNATURE-----
>
>
>


Mike Dvorkin

9/14/2006 5:15:00 AM

0

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



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 :)
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail....
>
> iD8DBQFFCKITKte2c0P8BH0RAtHAAJkBVZQ5yV8glhpACggeDPcH2RtCxgCeJRPv
> PCFuN23jhfTfvcv4NP3rDBY=
> =X7eL
> -----END PGP SIGNATURE-----
>
>


Randy Kramer

9/14/2006 1:40:00 PM

0

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

Randy Kramer

Lincoln Anderson

9/14/2006 4:17:00 PM

0

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

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

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


William Crawford

9/14/2006 4:29:00 PM

0

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

Rick DeNatale

9/14/2006 7:00:00 PM

0

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.

To me it seems quite natural.

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

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Lincoln Anderson

9/14/2006 9:03:00 PM

0

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

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

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


Jean-Denis Vauguet

9/14/2006 11:15:00 PM

0

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

Logan Capaldo

9/15/2006 1:47:00 PM

0

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