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.
To achieve the above for quick testing, it is much easier and readable
to just do:
s = “My name is #{‘Sam’} and my age is #{34}.”
Then, later, if the need for a hash or an object really does arise,
you can easily do:
p = { :name => ‘Sam’, :age => 34 }
s = “My name is #{p[:name]} and my age is #{p[:age]}.”
If you have say a Person object with proper accessors, it is even
nicer to read:
p = Person.new(:name=>‘Sam’, :age => 34)
s = “My name is #{p.name} and my age is #{p.age}.”
Better yet, if you call a simple function within, it is still very
readable. Ex:
s = “My name is #{p.name.upcase} and my age is #{p.age}.”
The %s syntax is anything but readable once you have more than 3
elements. Using a hash to try to clarify it seems to me more of a
crutch for languages that do not support ruby’s extremely powerful
“#{}” formating.
That syntax still forces me to look for meaning of the text format
string in two different places (in the location of the text and at the
end of it), instead of in a single place as I read the string, which
seems like a step backwards overall.