Why i can't get the value out of each block?

a=[1,2,0,10]
a.each.with_index{|item,idx|
begin
puts 10/item
rescue
print "it is my wrong ",item,idx,"\n"
x=item
end}
puts "x=",x

the output is :NameError: undefined local variable or method `x' for
main:Object
why i can't get the value out of each block?

···

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

Hi

In that case, x has scope of inside block.
if you want to reffer x out of blok, sould initialize before block.
like berow.

a=[1,2,0,10]
x=nil
a.each_with_index {|item, idx|
  begin
  puts 10 / item
  rescue
  print "it is my wrong ",item,idx,"\n"
  x=item
  end}
puts "x=",x

···

2010/7/28 Pen Ttt <myocean135@yahoo.cn>:

a=[1,2,0,10]
a.each.with_index{|item,idx|
begin
puts 10/item
rescue
print "it is my wrong ",item,idx,"\n"
x=item
end}
puts "x=",x

the output is :NameError: undefined local variable or method `x' for
main:Object
why i can't get the value out of each block?
--
Posted via http://www.ruby-forum.com/\.