[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String interpolation method?

John Carter

2/27/2008 10:54:00 PM

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


10 Answers

Jari Williamsson

2/27/2008 11:07:00 PM

0

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

Jari Williamsson

2/27/2008 11:13:00 PM

0

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

Mark Bush

2/27/2008 11:24:00 PM

0

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

James Gray

2/27/2008 11:32:00 PM

0

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.

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


yermej

2/27/2008 11:46:00 PM

0

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

I would do it thusly:

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

Trans

2/28/2008 2:40:00 AM

0


=> require 'facets/string/interpolate'
=> true
>> a = 'bra#{c}ket'
=> "bra\#{c}ket"
>> c = 4
=> 4
>> String.interpolate{a}
=> "bra4ket"

T.

Guby

2/29/2008 12:22:00 PM

0

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"

:)


On Feb 28, 2008, at 12:42 AM, John Carter wrote:

> 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 Carter Phone : (64)(3) 358 6639
> Tait Electronics F


Rob Biedenharn

2/29/2008 2:08:00 PM

0


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:
> 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"
>
> :)
>
> On Feb 28, 2008, at 12:42 AM, John Carter wrote:
>
>> 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 Carter Phone : (64)(3) 358 6639
>> Tait Electronics F

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



Guby

2/29/2008 5:32:00 PM

0

Sweet, that looks better!
Good to learn something new! :)

S

On Feb 29, 2008, at 11:07 AM, Rob Biedenharn wrote:

>
> 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:
>> 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"
>>
>> :)
>>
>> On Feb 28, 2008, at 12:42 AM, John Carter wrote:
>>
>>> 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 Carter Phone : (64)(3) 358 6639
>>> Tait Electronics F
>
> Rob Biedenharn http://agileconsult...
> Rob@AgileConsultingLLC.com
>
>
>


Trans

2/29/2008 6:27:00 PM

0



On Feb 29, 9:07 am, Rob Biedenharn <R...@AgileConsultingLLC.com>
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.