From: Morton Goldberg Date: 2007-07-10T14:24:18+09:00 Subject: Re: Trying to get dynamic output in console On Jul 10, 2007, at 12:45 AM, Nobuyoshi Nakada wrote: > Hi, > > At Tue, 10 Jul 2007 03:21:07 +0900, > Morton Goldberg wrote in [ruby-talk:258465]: >> 0.step(100, 10) do |i| > printf("\rProgress: %3.2d%%", i) >> $stdout.flush >> sleep(1) >> end >> puts > > The backslash before % is meaningless and just ignored. You > need two %'s to print one %. You are of course right: the backslash before the percent should be another percent. But in this case doubling the percent also turns out to be unnecessary. The following works (in Ruby 1.8.2) although one would think that it wouldn't: 0.step(100, 10) do |i| printf((i < 100 ? "\rProgress: %02d%" : "\rProgress: %3d%"), i) $stdout.flush sleep(1) end puts It seems to work because the single percent occurs at the end of the format string. Is this a minor bug in printf? Is it behavior inherited from C? Regards, Morton