From: "Jesús Gabriel y Galán" Date: 2010-12-07T19:27:17+09:00 Subject: Re: why catch block directly exeute On Tue, Dec 7, 2010 at 11:08 AM, Durga B. wrote: > 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. 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 = 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 = 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 => 4 If not thrown: irb(main):025:0> value = 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 => [1, 2, 3, 4] Returns the full array, which is what the each method returns. Jesus. Jesus.