Next iteration within a block

What is the most short/elegant way of ending the current iteration
within a block? My first thought was that next' will break the iteration and last evaluated expr will be returned to the caller, but surprisinglynil’ is returned this way, so we have (Ruby 1.6.8):

[‘bar’,‘quux’].map {‘foo’; next; 42}
=> [nil, nil]

instead of two foos in an array.

In a real code, when I don’t want to make the content of a block a
multibranched if statement (or something similar), I use something
like that:

#v+
aMethod do |item|
catch :next do
#blah
throw :next, special_result if special_condition
#blahblah
default_result
end
end
#v-

…but maybe there’s something better? (I really don’t think I found a
bug regarding `next’ behaviour :slight_smile:

···


We demand rigidly defined areas of doubt and uncertainty!

Try

[1,2].map {|i| next ‘b’ if i < 2; ‘a’}
=> [“b”, “a”]

Cheers

Dave

···

On Wednesday, Mar 12, 2003, at 23:17 US/Central, Wojciech Kaczmarek wrote:

What is the most short/elegant way of ending the current iteration
within a block? My first thought was that next' will break the iteration and last evaluated expr will be returned to the caller, but surprisingly nil’ is returned this way, so we have (Ruby 1.6.8):

[‘bar’,‘quux’].map {‘foo’; next; 42}
=> [nil, nil]

“Wojciech Kaczmarek” schatten@nospam.transilvania.eu.org schrieb im
Newsbeitrag news:b4p7bt.114.1@schatten.at.transilvania…

What is the most short/elegant way of ending the current iteration
within a block? My first thought was that next' will break the iteration and last evaluated expr will be returned to the caller, but surprisingly nil’ is returned this way, so we have (Ruby 1.6.8):

[‘bar’,‘quux’].map {‘foo’; next; 42}
=> [nil, nil]

What about

[‘bar’,‘quux’].map {‘foo’}

Why do you need the “next”?

In a real code, when I don’t want to make the content of a block a
multibranched if statement (or something similar), I use something
like that:

I assume you want to go trough the array, map values and omit some of
them. You can do

[‘bar’,‘quux’].map {|e| e == ‘bar’ ? nil : ‘foo’}.compact

or

[‘bar’,‘quux’].select{|e| e != ‘bar’ }.map{‘foo’}

Regards

robert

Hi,

···

At Thu, 13 Mar 2003 14:46:49 +0900, Dave Thomas wrote:

Try

[1,2].map {|i| next ‘b’ if i < 2; ‘a’}
=> [“b”, “a”]

Note that it’s a 1.7 (and later) feature.


Nobu Nakada

Seems that `next ’ syntax is not available in 1.6.x.

···

On Thu, 13 Mar 2003 14:46:49 +0900, Dave Thomas wrote:

What is the most short/elegant way of ending the current iteration
within a block? My first thought was that next' will break the iteration and last evaluated expr will be returned to the caller, but surprisingly nil’ is returned this way, so we have (Ruby 1.6.8):

[‘bar’,‘quux’].map {‘foo’; next; 42}
=> [nil, nil]

Try

[1,2].map {|i| next ‘b’ if i < 2; ‘a’}
=> [“b”, “a”]


This is not a healthy way of thinking.
(Emacs Psychiatrist)

What is the most short/elegant way of ending the current iteration within a
block? My first thought was that next' will break the iteration and last evaluated expr will be returned to the caller, but surprisingly nil’ is
returned this way, so we have (Ruby 1.6.8):
[‘bar’,‘quux’].map {‘foo’; next; 42}
=> [nil, nil]

What about
[‘bar’,‘quux’].map {‘foo’}

Why do you need the “next”?

I can assure you I don’t need it in the above example :->

I assume you want to go trough the array, map values and omit some of
them. You can do

[‘bar’,‘quux’].map {|e| e == ‘bar’ ? nil : ‘foo’}.compact
[‘bar’,‘quux’].select{|e| e != ‘bar’ }.map{‘foo’}

That’s good, but let’s imagine the selection predicate as a
fatty-several-times-nested statement – it’d be convenient to leave block
with a proper value sooner in some simpler cases.

Anyway, the syntax I dream of is avalilable in 1.7.

···

Robert Klemme bob.news@gmx.net wrote:

  • How can you make anyone into a star?
  • I dunno. I suppose you compress them right up small
    and they burst into this mass of flaming hydrogen?