How to delay "string" interpretation?

Suppose the following code:

t=’#{a} + #{b}’

a=‘a’;b=‘b’

How to cause ruby to interpret t as a “string” to get the result string
“a + b”?

To my knowledge, you won’t be able to do it in a
straightforward way.

Maybe consolidating the following code would do the job…

class ActiveString

def initialize(pattern, binding)
@pattern = pattern
@binding = binding
end

def to_s
Kernel.eval ‘"’ + @pattern + ‘"’, @binding
end

end

a, b = “a”, “b”
str = ActiveString.new(’#{a} + #{b}’, Kernel.binding)
puts str.to_s
a, b = “a2”, “b2”
puts str.to_s

Hope it helps,
B

Yes, that helps. Thanks a lot.

@Florian G.

The charm of LAMBEAU Bernard’s approach is, that #{expr} is much more
powerful, than a simple substitution. It enables to hardwire defaults
for non-existing, or nil variables.

But using a hash as parameter container is also interesting.

On Dec 7, 2010, at 1:11 PM, Fritz T. wrote:

Suppose the following code:

t=’#{a} + #{b}’

a=‘a’;b=‘b’

How to cause ruby to interpret t as a “string” to get the result string
“a + b”?

Well, instead of using the string interpolation syntax, you can use

String#%

which interpolates a String at runtime.

In Ruby 1.8.7 without additions, you can use it link this:

“My string contains a %s and a number as string: %s” % [“string”, 123]

You can also format that stuff, etc.

In Ruby 1.9.2 and 1.8.7 with certain additions (for example i18n), you
can use it like this:

“My String contrains a %{string} and a number as string: %{integer}” %
{:string => “string”, :integer => 123}

This allows you to define that string somewhere else and fill it in
later on.

Regards,
Florian


Florian G.

smtp: [email protected]
jabber: [email protected]
gpg: 533148E2

On Tue, Dec 7, 2010 at 1:11 PM, Fritz T. [email protected] wrote:

Suppose the following code:

t=‘#{a} + #{b}’

a=‘a’;b=‘b’

How to cause ruby to interpret t as a “string” to get the result string
“a + b”?

Turn it into a function of some kind:

def f(x,y)
“#{x} + #{y}”
end

x = f(“a”, “b”)

or

f = lambda {|x,y| “#{x} + #{y}”}
x = f[“a”, “b”]

Kind regards

robert

Robert K. wrote in post #966874:

f = lambda {|x,y| “#{x} + #{y}”}
x = f[“a”, “b”]

What happens in the 2nd line? f is a proc object, which gets passed an
array? Why gets the array split up?

On Tue, Dec 7, 2010 at 3:13 PM, Fritz T. [email protected] wrote:

The charm of LAMBEAU Bernard’s approach is, that #{expr} is much more
powerful, than a simple substitution. It enables to hardwire defaults
for non-existing, or nil variables.

You can do the same with the method approach I suggested.

But using a hash as parameter container is also interesting.

Without knowing more details about your application case I personally
find these suggested generic solutions overly complicated. YMMV
though.

Kind regards

robert

On Tue, Dec 7, 2010 at 2:24 PM, Fritz T. [email protected] wrote:

It isn’t getting passed an array, it is having it’s [] method invoked,
which
is just a synonym for the call method.

f = lambda {|x,y| “#{x} + #{y}”}

f.call( “a” , “b” )
f.[]( “a” , “b” )

f.call “a” , “b”
f.[] “a” , “b”

f.send “call” , “a” , “b”
f.send “[]” , “a” , “b”

http://www.ruby-doc.org/core/classes/Proc.html#M001555

Fritz T. wrote in post #966837:

Suppose the following code:

t=’#{a} + #{b}’

a=‘a’;b=‘b’

How to cause ruby to interpret t as a “string” to get the result string
“a + b”?

Here’s one option:

require ‘erb’
t = ‘<%= a %> + <%= b %>’
template = ERB.new(t)
a = ‘foo’
b = ‘bar’
puts template.result(binding)

erb is in the standard library. Erubis is an alternative implementation,
faster and more powerful, and really well documented.