For ... else ... end

Hi –

I’ve got the feeling that this thread is
drifting from the original problem (detect
successful looping) to the discussion about
some related but different problems.

One problem is the definition of “success” in looping. Personally I
don’t think there’s any such thing. This:

.each {|x| puts x}

is completely successful, as far as I can see. Python apparently
considers success to be indicated by ‘break’, which I think is a
style/taste position which I would not like to see given
language-level blessing.

[…]

Problem 1: Identify bypassing (e.g. empty array)
and declare coding that shall run in this case

if array.empty? … No, seriously :slight_smile: This includes testing results:

res = array.find_all { }
if res.empty? …

Problem 2: Identify interruption (by break or throw)
and declare coding that shall run in this case

I would say: if you want to branch this way, then use catch/throw with
your break (rather than trying to enhance ‘break’ so that it more or
less duplicates what catch/throw already does).

Problem 3: Identify full passing (no break)
and declare coding that shall run in this case.

This goes hand-in-hand with #2.

David Alan Black schrieb:

if array.empty?
puts “Empty array”
else
array.each {|e| puts e}
end

Ok, this solves problem 1 in some way (although it
does not ‘identify’ the bypassing itself, but checks
explicitly a precondition). So in relation to loops
you would check a condition twice:

if a > b
while a > b

end
else

end

I see this as a case of this:

if x
while y

where x and y happen to be the same test. I would say: it’s easier to
live with the occasional coincidence like this, than to introduce
language-level constructs to handle it.

[…]

So the complete example looks something like:

if array.empty?
print “Loop bypassed \n”
else
catch (:done) do
for i in array
if #
print “Interruption branch\n”
throw :done
end
print “Working in loop \n”
end
print “Loop terminated without break\n”
end
end

I would rewrite that like this:

if array.empty?
puts “Array is empty – no test”
else
if array.detect {|e| puts “Working in loop”; }
puts “Condition met”
else
puts “Condition not met”
end
end

Hum, the discussed construct with different
for-branches would simplify things … don’t you
think too?

I don’t know… it seems like a lot of what’s in this thread is very
complex, at least to my eye, compared to the simplicity Ruby offers.

David

···

On Wed, 10 Jul 2002, Dirk Detering wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi,

···

In message “Re: for … else … end” on 02/07/11, “Kontra, Gergely” kgergely@mlabdial.hit.bme.hu writes:

What is the reason one cannot pass multiply block simply

Because I don’t know how to receive/call multiple block.
Besides, I’m not sure whether YACC allows multiple block syntax.

						matz.

Thu, 11 Jul 2002 08:31:22 +0900, Yukihiro Matsumoto matz@ruby-lang.org pisze:

What is the reason one cannot pass multiply block simply

Because I don’t know how to receive/call multiple block.

By putting ‘&proc1, &proc2’ in parameter list.

Besides, I’m not sure whether YACC allows multiple block syntax.

I guess it would like them: if “optional block” is recognizable
after plain args, it should also be recognizable after a block
already occurred.

···


__("< Marcin Kowalczyk
__/ qrczak@knm.org.pl
^^ Blog człowieka poczciwego.

Marcin ‘Qrczak’ Kowalczyk wrote:

Thu, 11 Jul 2002 08:31:22 +0900, Yukihiro Matsumoto matz@ruby-lang.org pisze:

Besides, I’m not sure whether YACC allows multiple block syntax.

I guess it would like them: if “optional block” is recognizable
after plain args, it should also be recognizable after a block
already occurred.

Tcl handles it somehow:

 if { $x > 0 } { puts positive } { puts nonpositive }

It might be nice to be able to write this kind of thing in Ruby. But I
think this came up on ruby-talk a while ago and it didn’t fly.

But in Tcl, those “blocks” are syntactically just strings. The “if”
command chooses to treat its arguments as code, but that happens after
the command is parsed. Tcl doesn’t have any kind of block syntax at
all; it has string syntax, and some of its commands treat strings as
code.

Regards,

Jeremy Henty

···

In article 3D2F06C2.7080603@path.berkeley.edu, Joel VanderWerf wrote:

Marcin ‘Qrczak’ Kowalczyk wrote:

Thu, 11 Jul 2002 08:31:22 +0900, Yukihiro Matsumoto matz@ruby-lang.org pisze:

Besides, I’m not sure whether YACC allows multiple block syntax.

I guess it would like them: if “optional block” is recognizable
after plain args, it should also be recognizable after a block
already occurred.

Tcl handles it somehow:

 if { $x > 0 } { puts positive } { puts nonpositive }