How to delay "string" interpretation?

F

Fritz Trapper

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

Florian Gilcher

Suppose the following code:
=20
t=3D'#{a} + #{b}'
...
a=3D'a';b=3D'b'
=20
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 =3D> "string", :integer =3D> 123}

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

Regards,
Florian

--
Florian Gilcher

smtp: (e-mail address removed)
jabber: (e-mail address removed)
gpg: 533148E2
 
F

Fritz Trapper

@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.
 
R

Robert Klemme

Suppose the following code:

=A0 t=3D'#{a} + #{b}'
=A0 ...
=A0 a=3D'a';b=3D'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 =3D f("a", "b")

or

f =3D lambda {|x,y| "#{x} + #{y}"}
x =3D f["a", "b"]

Kind regards

robert


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

Fritz Trapper

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

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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

Robert Klemme

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
 
B

Brian Candler

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.
 

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

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top