Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > String interpolation method?

Reply
Thread Tools

String interpolation method?

 
 
John Carter
Guest
Posts: n/a
 
      02-27-2008
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 : http://www.velocityreviews.com/forums/(E-Mail Removed)
New Zealand


 
Reply With Quote
 
 
 
 
James Gray
Guest
Posts: n/a
 
      02-27-2008
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


 
Reply With Quote
 
 
 
 
yermej
Guest
Posts: n/a
 
      02-27-2008
On Feb 27, 4:53 pm, John Carter <(E-Mail Removed)> 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 : (E-Mail Removed)
> New Zealand


I would do it thusly:

a = "bra%sket"
c = ' see '
a % c
=> "bra see ket"
 
Reply With Quote
 
Trans
Guest
Posts: n/a
 
      02-28-2008

=> require 'facets/string/interpolate'
=> true
>> a = 'bra#{c}ket'

=> "bra\#{c}ket"
>> c = 4

=> 4
>> String.interpolate{a}

=> "bra4ket"

T.

 
Reply With Quote
 
Guby
Guest
Posts: n/a
 
      02-29-2008
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



 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      02-29-2008

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://agileconsultingllc.com
(E-Mail Removed)



 
Reply With Quote
 
Guby
Guest
Posts: n/a
 
      02-29-2008
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://agileconsultingllc.com
> (E-Mail Removed)
>
>
>



 
Reply With Quote
 
Trans
Guest
Posts: n/a
 
      02-29-2008


On Feb 29, 9:07 am, Rob Biedenharn <(E-Mail Removed)>
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Yet another string interpolation function... MonkeeSage Python 0 03-04-2007 08:38 AM
modifying html input date for mysql, reg ex or string interpolation? Kun Python 2 04-11-2006 11:17 PM
Perl/Ruby string interpolation Aiden Humphreys Java 5 05-22-2005 06:37 PM
yet another recipe on string interpolation Michele Simionato Python 8 11-08-2004 08:11 PM
YA string interpolation and printing idea Paul Rubin Python 4 01-19-2004 04:52 PM



Advertisments