I'm trying to make a function act as a counter, but I can't seem to get
it to work. Here's what I've tried:
def count
counter = 1
def count
proc {counter += 1}.call
end
counter
end
I was hoping for this:
count => 1
count => 2
count => 3
Instead, I got this:
count => 1
count => NoMethodError: undefined method '+' for nil:NilClass
Isn't the idea of a closure that everything in the block is executed in
the same scope it was defined? It seems that the name 'counter' is
referenced under the name 'count', and so when I redefine 'count', the
'counter' variable gets lost. Anyone know what's really happening here?
Thanks,
Greg
CONFIDENTIALITY NOTICE
This message and any included attachments
are from Cerner Corporation and are intended
only for the addressee. The information
contained in this message is confidential and
may constitute inside or non-public information
under international, federal, or state
securities laws. Unauthorized forwarding,
printing, copying, distribution, or use of such
information is strictly prohibited and may be
unlawful. If you are not the addressee, please
promptly delete this message and notify the
sender of the delivery error by e-mail or you
may call Cerner's corporate offices in Kansas
City, Missouri, U.S.A at (+1) (816)221-1024.
---------------------------------------- --