You can do it with regular expressions or a parser, but you
shouldn’t…it will be much slower (and I mean like a gabazillion
times slower), and almost certainly less robust (e.g., handling nested
symbols), than the built-in interpolation syntax. Why do you need a
different syntax?
Sorry, it seems I totally misunderstood your question … which I
don’t quite understand now after reading it again. You could use gsub
(maybe in conjunction with a block to look up variable values) but
why …
It’s not a dire need, but it would definitely be useful. For example,
suppose you are constructing a string that generates ruby code. You
will need a way to tell the parser that #{foobar} is no longer
intended as a variable placeholder.
You could, of course, change the entire string into an uninterpolated
string, but that eliminates the ability to specify variables entirely.
Just to let you know this is not an original idea that I cooked up out
of thin air, see how Python supports this feature in:
http://www.python.org/dev/peps/pep-0292/
and search for the text:
define a delimiter character other than '$'
ALTERNATIVE QUESTION:
I’ve used ERB before, does it enable you to specify alternate syntaxes
for the variable placeholders? Template::Toolkit (as an example) does
allow this, if you want more details on what I am asking about, take a
look at:
I’ve used ERB before, does it enable you to specify alternate syntaxes
for the variable placeholders? Template::Toolkit (as an example) does
allow this, if you want more details on what I am asking about, take a
look at:
Thanks for your responses so far.
Hmm…what about using %q{} or escaping “#”? You can emulate the
python Template class (which I have never actually used in live python
code, since % has always worked for my needs); but since it will be
slower (benchmark Template.substitute against % in python), and you
can do the same thing in different ways, it seems pointless to me.
But, for completeness, here is an example:
class Template < String
def substitute(context=nil)
if /@{(.+?)}/ =~ self
self.gsub("@{#{$1}}", eval($1, context))
else
self
end
end
end
blah = “foo”
t = Template.new(“Hello @{blah}”).substitute(binding)
p t # => “Hello foo”
t = Template.new(“Hello world”).substitute
p t # => “Hello world”
Some years ago amrita or so was “more” actively developed. IIRC misen
was an effort to create a template engine that provides the kind of
flexibility you’re looking for. Or maybe not. Both projects seem
rather moribund now though.
If you use a gsub-based solution (as proposed by Scytrin), you might
also want to think about how to escape the markers.
It’s not a dire need, but it would definitely be useful. For example, http://www.python.org/dev/peps/pep-0292/
look at:
But, for completeness, here is an example:
blah = “foo”
t = Template.new(“Hello @{blah}”).substitute(binding)
p t # => “Hello foo”
t = Template.new(“Hello world”).substitute
p t # => “Hello world”
Regards,
Jordan
Yes, PEP292 Template strings are probably rarely ever used in
production, or even at all,
(that’s speculation here, but doubtless accurate) and the performance
issue
and other points you mentioned are of course well stated. There
are ,however,
instances where this specific feature is actually very useful and a
huge
time-saver.
Nevertheless, thanks for the responses and overview.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.