How to exit from outerblock

I have 2 blocks .i want exit innerblock and outer block if condition
failed

gnerally break will exit from innerblock

a=[1,2,3,4,5,6]
b=[5,2,3]
a.each do |e|
puts e
b.each do |q|
break if q==e
end
end

here if q==e.it should exit from b and a blocks

On Mon, Feb 6, 2012 at 1:43 AM, Lucky Nl [email protected] wrote:

b.each do |q|
break if q==e
end
end

here if q==e.it should exit from b and a blocks


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

a=[1,2,3,4,5,6]
b=[5,2,3]
catch :done do
a.each do |e|
puts e
b.each do |q|
throw :done if q==e
end
end
end

>> 1

>> 2

On Mon, Feb 6, 2012 at 8:43 AM, Lucky Nl [email protected] wrote:

b.each do |q|
break if q==e
end
end

here if q==e.it should exit from b and a blocks

“e.it” is a Groovyism. We’re in the realms of Ruby here. :slight_smile:

Question is, what do you want to achieve? Chances are that you want
#find for this use case:

irb(main):001:0> a=[1,2,3,4,5,6]
=> [1, 2, 3, 4, 5, 6]
irb(main):002:0> b=[5,2,3]
=> [5, 2, 3]
irb(main):003:0> a.find do |e|
irb(main):004:1* puts e
irb(main):005:1> b.include? e
irb(main):006:1> end
1
2
=> 2

But what problem are you trying to solve?

Kind regards

robert

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