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 C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : [email protected]
New Zealand
Jari W. wrote:
the use of ‘’ instead of “”.
eval “”#{a}""
=> “bra see ket”
Any better way?
eval a
I spoke too soon - my suggestion will not work…
Best regards,
Jari W.
John C. 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).
On Feb 27, 4:53 pm, John C. [email protected] wrote:
=> “bra see ket”
Any better way?
Thanks,
John C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : [email protected]
New Zealand
I would do it thusly:
a = “bra%sket”
c = ’ see ’
a % c
=> “bra see ket”
On Feb 27, 2008, at 4:53 PM, John C. 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.
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 G. II
=> require ‘facets/string/interpolate’
=> true
a = ‘bra#{c}ket’
=> “bra#{c}ket”
c = 4
=> 4
String.interpolate{a}
=> “bra4ket”
T.
On Thu, 28 Feb 2008, Trans wrote:
=> require ‘facets/string/interpolate’
Hmm…
Very neat in application…
String.interpolate{a}
but…
def interpolate(&str)
eval "%{#{str.call}}", str.binding
end
…equally nasty in implementation.
Ah well, clearly then I wasn’t missing something… there is no
standard method.
Thanks,
John C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : [email protected]
New Zealand
Not that it is a lot nicer, but just another solution… and a little
bit on side of what you wanted, but:
a = lambda{|c| “bra#{c}ket”}
a.call(" see ")
=> “bra see ket”
If you want it to look even nicer, write it like:
a = lambda{|c| “bra#{c}ket”}
a[" see "]
=> “bra see ket”
-Rob
On Feb 29, 2008, at 7:21 AM, Guby wrote:
Thanks,
John C. Phone : (64)(3) 358 6639
Tait Electronics F
Rob B. http://agileconsultingllc.com
[email protected]
Sweet, that looks better!
Good to learn something new!
S
On Feb 29, 9:07 am, Rob B. [email protected]
wrote:
If you want it to look even nicer, write it like:
a = lambda{|c| “bra#{c}ket”}
a[" see "]
=> “bra see ket”
The only trouble here is that you get the binding of where you defined
the lambda, rather than the one in which you ultimately evaluate it
in.
With 1.9 I think we can use instance_exec to handle that however –
that being the case, the String::interpolate method I demoed above
could be improved.
T.