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"?

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Tue, Dec 7, 2010 at 1:11 PM, Fritz Trapper <ajfrenzel@web.de> 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"?

--
Posted via http://www.ruby-forum.com/\.

Yes, that helps. Thanks a lot.

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Dec 7, 2010, at 1:11 PM, Fritz Trapper 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"?

--
Florian Gilcher

smtp: flo@andersground.net
jabber: Skade@jabber.ccc.de
gpg: 533148E2

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

···

On Tue, Dec 7, 2010 at 1:11 PM, Fritz Trapper <ajfrenzel@web.de> 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"?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

@Florian Gilcher

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.

···

--
Posted via http://www.ruby-forum.com/.

Fritz Trapper 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.

···

--
Posted via http://www.ruby-forum.com/\.

Robert Klemme 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?

···

--
Posted via http://www.ruby-forum.com/\.

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 3:13 PM, Fritz Trapper <ajfrenzel@web.de> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

···

On Tue, Dec 7, 2010 at 2:24 PM, Fritz Trapper <ajfrenzel@web.de> wrote:

Robert Klemme 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?

--
Posted via http://www.ruby-forum.com/\.