On Thu, Dec 10, 2009 at 9:36 AM, Robert Citek <
robert...@gmail.com> wrote:
> On Wed, Dec 9, 2009 at 11:39 PM, Ed Howland <
ed.ho...@gmail.com> wrote:
>> I assum you wat to line number a file?
>
> I was more interested in how to initialize a variable from within the
> the loop created by using the -n command-line option. Numbering lines
> was the simplest example I could think of.
And I thought I understood what ruby was doing. This works, except
that it creates a variable c external to the block:
$ ruby -e 'c=0 ; (1..3).each {|x| c+=1 ; puts c } ; puts c'
1
2
3
3
Although c is now local to the block, the variable c is not incremented:
$ ruby -e '(1..3).each {|x| c=(c||0)+1 ; puts c } ; puts c'
1
1
1
-e:1: undefined local variable or method `c' for main:Object (NameError)
How can one create a variable that is local to a block?
Regards,
- Robert