From: Ezra Zygmuntowicz Date: 2007-06-07T10:47:58+09:00 Subject: Re: Globals not incrementing inside block On Jun 6, 2007, at 6:34 PM, Todd A. Jacobs wrote: > I have the following snippet: > > at_exit do > $total_count, $daily_count = 0, 0 > IO.foreach(my_logfile) do |line| > $total_count.next > $daily_count.next if line =~ /^#{Time.now.strftime('%Y%m%d')}/ > end > printf("\ns Total: %d\ns Today:%d\n", $total_count, > $daily_count) > end > > I assumed I had to use globals, since otherwise the block would treat > the counters as local variables, but I have the same problem either > way: > the counters never increase. They still both read zero at the end > of the > script, even though some strategic puts statements show that the > file is > successfully being read. > > What newbie mistake am I making now? > > -- > "Oh, look: rocks!" > -- Doctor Who, "Destiny of the Daleks" > $total_count.next does not increment $total_count try this instead: $total_count += 1 Cheers- -- Ezra Zygmuntowicz -- Lead Rails Evangelist -- ez@engineyard.com -- Engine Yard, Serious Rails Hosting -- (866) 518-YARD (9273)