7stud -- <bbxx789_05ss / yahoo.com> wrote:
>
>1)
>class ABC
>    def do_something
>       number = 10
>       puts number
>    end
>end
>
>ABC.new.do_something
>
>--output:--
>10
>
>
>2)
>class ABC
>    def do_something
>        3.times do |x|
>            number = 10
>        end
>        puts number
>    end
>end
>
>ABC.new.do_something
>
>--output:--
>Line 6:in `do_something': undefined local variable or method `number'
>for #<ABC:0x401bfaa4> (NameError)
>  from t.rb:10
>
>Why does ruby get confused by the setter v. local variable assignment
>when adding a block?

There's no "setter" here.  Both of those are local variable assignments. In
the second example, the variable is created local to the block.  After the
block ends, the variable ceases to exist.

If you want the variable to exist in the outher block, just create it
there:

class ABC
    def do_something
        number = 0
        3.times do |x|
            number = 10
        end
        puts number
    end
end

ABC.new.do_something
-- 
Tim Roberts, timr / probo.com
Providenza & Boekelheide, Inc.