From: Gavin Sinclair Date: 2004-06-02T17:15:39+09:00 Subject: Re: What is the equivalent of Python's '%s' % 'MyString'? > nobu.nokada@softhome.net wrote in message > news:<200406020151.i521pOHY005774@sharui.nakada.niregi.kanuma.tochigi.jp>... >> Hi, >> >> At Wed, 2 Jun 2004 03:43:42 +0900, >> Sam Sungshik Kong wrote in [ruby-talk:102028]: >> > s = "My name is %(name)s and my age is %(age)d." % {"name": "Sam", >> "age": 34} >> >> > I know that ruby has "#{name}" expression. >> > But that requires a variable named "name" in advance. >> > I want to bind the format string and data later. >> >> I proposed that feature once in [ruby-dev:16351], though >> rejected, but still I think that it would be useful for I18N. >> Is it worth for RCR? > > Do you have an example on how would you use this? I ask because I would > also vote strongly against it. > > One of the things that I really loved about ruby while learning it was > its "#{}" syntax, as it seemed to me a much more consistent way of > achieving what sprintf, $, %, etc. have been trying for years to > achieve in languages like C, perl, python, etc. Incorrect thinking, I'm afraid. printf and friends are better than #{} for *formatted* printing; i.e. specifying field widths, precision, and so on. In fact, #{} doesn't even attempt to do formatting. So there's room in the world for both. An example: format = " %-25(name)s | %5(age)d | %10.2(salary)f \n" records.each do |r| printf format, :name => r.name, :age => r.age, :salary => r.salary end That's superior to the existing printf capabilities because it's clearer and the order of the parameters are independent of the format. Cheers, Gavin