why catch block directly exeute

D

Durga B.

catch :)finish) do
puts "Generated 1000 random numbers without generating 123!"
end

The above code if not throw or any thing call and execute directly .
How is it works please any one explain and if possible give a example
for throw and catch block.
 
J

Jesús Gabriel y Galán

catch :)finish) do
=A0 =A0puts "Generated 1000 random numbers without generating 123!"
=A0 =A0end

The above code if not throw or any thing call and execute directly .
How is it works please any one explain and if possible give a example
for throw and catch block.

http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_m_kernel.html#Kernel.=
catch
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_m_kernel.html#Kernel.=
throw

It's how catch works. It executes the block. If within the block, the
symbol you pass to catch is thrown, catch returns the value passed to
throw (second argument or nil). If not thrown, catch terminates
normally, and returns the value of the last expression in the block:

value =3D catch:)finish) do
[1,2,3,4].each do |v|
throw:)finish,v) if v > 3
puts v
end
end

irb(main):019:0> value =3D catch:)finish) do
irb(main):020:1* [1,2,3,4].each do |v|
irb(main):021:2* throw:)finish, v) if v > 3
irb(main):022:2> puts v
irb(main):023:2> end
irb(main):024:1> end
1
2
3
=3D> 4

If not thrown:

irb(main):025:0> value =3D catch:)finish) do
irb(main):026:1* [1,2,3,4].each do |v|
irb(main):027:2* throw:)finish, v) if v > 10
irb(main):028:2> puts v
irb(main):029:2> end
irb(main):030:1> end
1
2
3
4
=3D> [1, 2, 3, 4]

Returns the full array, which is what the each method returns.

Jesus.

Jesus.
 
D

Durga B.

Thank you very much Jes=C3=BAs Gabriel y Gal=C3=A1n but some confusion pl=
ease
explain with different catch and throw,not include same both are same
block take as a different ,please.

-- =

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

flebber

J

Jesús Gabriel y Galán

Thank you very much Jes=FAs Gabriel y Gal=E1n but some confusion please
explain with different catch and throw,not include same both are same
block take as a different ,please.

I'm not sure I understand your question. The way it works is that when
you throw, Ruby looks up through the stack for a catch with the same
symbol. If it finds it, the block passed to catch is terminated
immediately, and the block returns the value passed to throw. This is
useful to exit from a deeply nested structure, for example (based on
http://phrogz.net/programmingruby/tut_exceptions.html) :

def processValue value
throw :quit if value =3D~ /quit!/i
value.downcase
end

def promptAndGet(prompt)
print prompt
value =3D gets.chomp
processValue value
end

catch :quit do
name =3D promptAndGet "Name: "
age =3D promptAndGet "Age: "
sex =3D promptAndGet "Sex: "
#do_something_with name, age, sex
end

You can nest catch blocks:

catch:)first_level) do
puts "at first level"
catch:)second_level) do
puts "At second level"
if (rand(2) =3D=3D 0)
throw :second_level
else
throw :first_level
end
puts "still in second level"
end
puts "still in first level"
end

Jesus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top