change the symbol in an interpolated string value?

S

scooterm

PROBLEM:
I would like to be able to change the symbol in an interpolated string
value.
For example:

# before
whoever = "World";
message = "Hello #{whoever}";
puts message;

# after
whoever = "World";
## message = "Hello @{whoever}"; ## one arbitrary example
## message = "Hello ${whoever}"; ## another example
## message = "Hello ???{whoever}"; ## yet another
## message = "Hello ___whoever___"; ## or even yet another, no
braces this time
puts message;

I would like to be able to make it so any of the commented-out
versions could work, just as the "before" version works.

What's the ruby-way of doing this?
 
M

MonkeeSage

PROBLEM:
I would like to be able to change the symbol in an interpolated string
value.
For example:

# before
whoever = "World";
message = "Hello #{whoever}";
puts message;

# after
whoever = "World";
## message = "Hello @{whoever}"; ## one arbitrary example
## message = "Hello ${whoever}"; ## another example
## message = "Hello ???{whoever}"; ## yet another
## message = "Hello ___whoever___"; ## or even yet another, no
braces this time
puts message;

I would like to be able to make it so any of the commented-out
versions could work, just as the "before" version works.

What's the ruby-way of doing this?

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?

Regards,
Jordan
 
S

scooterm

Why do you need a
different syntax?

Regards,
Jordan

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:

http://template-toolkit.org/docs/manual/Syntax.html

Thanks for your responses so far.
 
S

scooterm

(maybe in conjunction with a block to look up variable values) but

I think you understood, take a look at my reply to Monkee for more
details.

Thanks
 
P

Paul Stickney

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:

Short answer: No.
 
M

MonkeeSage

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:

http://template-toolkit.org/docs/manual/Syntax.html

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"

Regards,
Jordan
 
T

tho_mica_l

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.
 
S

scooterm

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"

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top