[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Clean way to dynamically expand string with "#{expression}"?

Taisuke Yamada

4/22/2005 5:32:00 AM

Hi.

I'm trying to find a clean way to expand string with "#{...}".
What I want to do is essentially as follows:

data = 123
expr = 'data = #{data}'

puts dosomething(expr)

and get 'data = 123' as a result. Actual content of 'expr' can
be anything, as long as it is a String object.

I came up with following:

puts eval(%Q{"#{expr.gsub('"', '\"')}"})

But this is ugly, and I'm wondering if there's some other,
cleaner way to do the same thing.

Can anyone enlighten me?

Best Regards,
--
Taisuke Yamada
7 Answers

dblack

4/22/2005 9:45:00 AM

0

Denis Mertz

4/22/2005 11:10:00 AM

0

David A. Black wrote:

> Hi --
>
> On Fri, 22 Apr 2005, Taisuke Yamada wrote:
>
>> Hi.
>>
>> I'm trying to find a clean way to expand string with "#{...}".
>> What I want to do is essentially as follows:
>>
>> data = 123
>> expr = 'data = #{data}'
>>
>> puts dosomething(expr)
>>
>> and get 'data = 123' as a result. Actual content of 'expr' can
>> be anything, as long as it is a String object.
>
> Can't you just double-quote your string?
>
> expr = "data = #{data}"
>
>
> David
>


I guess what Taisuke Yamada want is 'interpolation on demand', as opposed to
interpolation when the string is first read by the ruby interpreter. This is
needed when you want to interpolate strings that are builded from external
sources. This subject was actually already discussed in ruby-talk/129959
(delayed string interpolation)

Denis



Ara.T.Howard

4/22/2005 2:06:00 PM

0

Taisuke Yamada

4/22/2005 4:42:00 PM

0

Yes, what I was looking for is 'perfect interpolation on demand'
that'll work whatever content is in interpolated string. I guess

eval("<<END_REEVAL\n" + self + "\nEND_REEVAL\n", b)

is the best (and robust) solution available as you can choose
random string that probably won't show up in interpolated string.

Many interpolation code, including mine and several people
suggested will break if '"' or certain string is contained
in string to interpolate.

Practically, it doesn't need to be perfect, but it was my
technical interest that there may be better way to do it.
Above code is cleaner and more robust, so I'll go with it.
Thanks for the pointer.

BTW, I'm using this on-demand interpolation to supply options
dynamically to random shell commands read from configuration
file. Using template processing library was bit overkill, so
I went with eval-based one liner to do the job.

> I guess what Taisuke Yamada want is 'interpolation on demand', as opposed to
> interpolation when the string is first read by the ruby interpreter. This is
> needed when you want to interpolate strings that are builded from external
> sources. This subject was actually already discussed in ruby-talk/129959
> (delayed string interpolation)

Denis Mertz

4/23/2005 12:23:00 PM

0

Taisuke Yamada wrote:

> Yes, what I was looking for is 'perfect interpolation on demand'
> that'll work whatever content is in interpolated string. I guess
>
> eval("<<END_REEVAL\n" + self + "\nEND_REEVAL\n", b)
>
> is the best (and robust) solution available as you can choose
> random string that probably won't show up in interpolated string.

Ah the quest for perfection, we are still not there :

irb(main):001:0> str = "blah\nEND_REEVAL\n catch me if you can"
=> "blah\nEND_REEVAL\n catch me if you can"
irb(main):002:0> eval("<<END_REEVAL\n" + str + "\nEND_REEVAL\n", binding)
NameError: undefined local variable or method `can' for main:Object
from (irb):5
from (irb):2


i.e. it will fail for every string containing the "\nEND_REEVAL\n" string
delimiter.

>
> Many interpolation code, including mine and several people
> suggested will break if '"' or certain string is contained
> in string to interpolate.
>
> Practically, it doesn't need to be perfect, but it was my
> technical interest that there may be better way to do it.
> Above code is cleaner and more robust, so I'll go with it.
> Thanks for the pointer.
>

I do share that technical interest. I really wonder if there is a one liner
for 'perfect interpolation on demand'. Everything i can think of is a bit
longer. Let the quest continue :)

Ciao

Denis



Douglas Livingstone

4/23/2005 1:14:00 PM

0

On 4/23/05, dm1 <dmertz@online.de> wrote:
> >
> > Many interpolation code, including mine and several people
> > suggested will break if '"' or certain string is contained
> > in string to interpolate.
> >
> > Practically, it doesn't need to be perfect, but it was my
> > technical interest that there may be better way to do it.
> > Above code is cleaner and more robust, so I'll go with it.
> > Thanks for the pointer.
> >
>
> I do share that technical interest. I really wonder if there is a one liner
> for 'perfect interpolation on demand'. Everything i can think of is a bit
> longer. Let the quest continue :)
>

The perfection might look something like this:

"string with #{data}" == 'string with #{data}'.interpolate

"abc" would just be syntax sugar for 'abc'.interpolate

I don't know how to express that in ruby though :(

Douglas



dblack

4/23/2005 2:08:00 PM

0