String interpolation method?

Ok, I'm being stupid probably...

But I can't spot a method to do string interpolation.

I have a string...

a = 'bra#{c}ket'

The variable c isn't available at the stage of setting that up. Hence
the use of '' instead of "".

The variable will be available later....

At that stage I want to do something like...

c=' see '
a.interpolate

The closest I can get is a bit fugly...

eval "\"#{a}\""
=> "bra see ket"

Any better way?

Thanks,

John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : john.carter@tait.co.nz
New Zealand

John Carter wrote:

Ok, I'm being stupid probably...

But I can't spot a method to do string interpolation.

I have a string...

a = 'bra#{c}ket'

The variable c isn't available at the stage of setting that up. Hence
the use of '' instead of "".

The variable will be available later....

At that stage I want to do something like...

c=' see '
a.interpolate

The closest I can get is a bit fugly...

eval "\"#{a}\""
=> "bra see ket"

Any better way?

eval a

Best regards,

Jari Williamsson

John Carter wrote:

eval "\"#{a}\""
=> "bra see ket"

Any better way?

Not better. Just different:

eval '"%s"'%(a)

Does the same thing but without needing the backslashes.

I don't see any other way really. You must have a double eval in some
manner. One to get the value of a and one to evaluate the result. The
first eval can be explicit (as in your example with #{}) or implicit (as
in my example).

···

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

You want a templating solution, like the ERb code that ships with Ruby:

   #!/usr/bin/env ruby -wKU

   require "erb"

   template = ERB.new("bra<%= c %>ket")

   # and later...
   c = "c"
   p template.result

   __END__

Hope that helps.

James Edward Gray II

···

On Feb 27, 2008, at 4:53 PM, John Carter wrote:

Ok, I'm being stupid probably...

But I can't spot a method to do string interpolation.

I have a string...

a = 'bra#{c}ket'

The variable c isn't available at the stage of setting that up.

I would do it thusly:

a = "bra%sket"
c = ' see '
a % c
=> "bra see ket"

···

On Feb 27, 4:53 pm, John Carter <john.car...@tait.co.nz> wrote:

Ok, I'm being stupid probably...

But I can't spot a method to do string interpolation.

I have a string...

a = 'bra#{c}ket'

The variable c isn't available at the stage of setting that up. Hence
the use of '' instead of "".

The variable will be available later....

At that stage I want to do something like...

c=' see '
a.interpolate

The closest I can get is a bit fugly...

eval "\"#{a}\""
=> "bra see ket"

Any better way?

Thanks,

John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : john.car...@tait.co.nz
New Zealand

=> require 'facets/string/interpolate'
=> true

a = 'bra#{c}ket'

=> "bra\#{c}ket"

c = 4

=> 4

String.interpolate{a}

=> "bra4ket"

T.

Jari Williamsson wrote:

John Carter wrote:

Ok, I'm being stupid probably...

But I can't spot a method to do string interpolation.

I have a string...

a = 'bra#{c}ket'

The variable c isn't available at the stage of setting that up. Hence
the use of '' instead of "".

The variable will be available later....

At that stage I want to do something like...

c=' see '
a.interpolate

The closest I can get is a bit fugly...

eval "\"#{a}\""
=> "bra see ket"

Any better way?

eval a

I spoke too soon - my suggestion will not work...

Best regards,

Jari Williamsson