[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Interpolating a string loaded from a text file

John Elrick

4/6/2005 3:31:00 PM

Hoping you guys can help out here. Is there any way to interpolate a
string that is loaded dynamically?

I know that in Ruby I can do this:

my_message = 'Hello World'
my_template = "The message is #my_message"
puts my_template <== The message is Hello World

But what I want to do is

my_message = 'Hello World'
my_template =
File.readlines('my_template_file.txt').HOW-CAN-I-FORCE-INTERPOLATION???
puts my_template <== The message is Hello World


Anyone have any ideas? Or am I missing something blatantly obvious?

TIA

John Elrick

9 Answers

Robert Klemme

4/6/2005 3:46:00 PM

0


"John Elrick" <john.elrick@gmail.com> schrieb im Newsbeitrag
news:1112801469.561320.13590@z14g2000cwz.googlegroups.com...
> Hoping you guys can help out here. Is there any way to interpolate a
> string that is loaded dynamically?
>
> I know that in Ruby I can do this:
>
> my_message = 'Hello World'
> my_template = "The message is #my_message"
> puts my_template <== The message is Hello World
>
> But what I want to do is
>
> my_message = 'Hello World'
> my_template =
> File.readlines('my_template_file.txt').HOW-CAN-I-FORCE-INTERPOLATION???
> puts my_template <== The message is Hello World
>
>
> Anyone have any ideas? Or am I missing something blatantly obvious?

How about:

eval "%Q{" + File.read('my_template_file.txt') + "}"

Kind regards

robert

James Gray

4/6/2005 3:48:00 PM

0

On Apr 6, 2005, at 10:34 AM, John Elrick wrote:

> Hoping you guys can help out here. Is there any way to interpolate a
> string that is loaded dynamically?
>
> I know that in Ruby I can do this:
>
> my_message = 'Hello World'
> my_template = "The message is #my_message"
> puts my_template <== The message is Hello World

Only if you replace #my_message with #{my_message}. ;)

> But what I want to do is
>
> my_message = 'Hello World'
> my_template =
> File.readlines('my_template_file.txt').HOW-CAN-I-FORCE-INTERPOLATION???
> puts my_template <== The message is Hello World
>
>
> Anyone have any ideas? Or am I missing something blatantly obvious?

Well, there's always search and replace:

template.gsub(/#\{\s*(\w+)\s*\}/) { # do something with $1 here... }

I think that works pretty well with a Hash, keyed by the $1 values to
interpolate.

Option two is to call eval(). This executes Ruby code and returns the
results. This naturally has security concerns, if you aren't in
control of the template.

Finally, you can make use of Ruby's standard template library, ERb:

http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classe...

Hope that helps.

James Edward Gray II



vrajmohan

4/6/2005 3:51:00 PM

0

Will using Kernel#sprintf work achieve the same thing?
--Vraj Mohan


Robert Klemme

4/6/2005 3:52:00 PM

0


"Vraj Mohan" <vrajmohan@comcast.net> schrieb im Newsbeitrag
news:42540521.7030801@comcast.net...
> Will using Kernel#sprintf work achieve the same thing?
> --Vraj Mohan
>

No.

robert

Ilmari Heikkinen

4/6/2005 4:35:00 PM

0

ke, 2005-04-06 kello 18:49, Robert Klemme kirjoitti:
> "John Elrick" <john.elrick@gmail.com> schrieb im Newsbeitrag
> news:1112801469.561320.13590@z14g2000cwz.googlegroups.com...
> > my_message = 'Hello World'
> > my_template =
> > File.readlines('my_template_file.txt').HOW-CAN-I-FORCE-INTERPOLATION???
> > puts my_template <== The message is Hello World
> >
> >
> > Anyone have any ideas? Or am I missing something blatantly obvious?
>
> How about:
>
> eval "%Q{" + File.read('my_template_file.txt') + "}"
>

echo "}; 10.times{ puts 'do not trust external data'" > my_template_file.txt

Cheers :)




Robert Klemme

4/6/2005 5:28:00 PM

0


"Ilmari Heikkinen" <kig@misfiring.net> schrieb im Newsbeitrag
news:1112805304.14517.5.camel@jugend...
> ke, 2005-04-06 kello 18:49, Robert Klemme kirjoitti:
> > "John Elrick" <john.elrick@gmail.com> schrieb im Newsbeitrag
> > news:1112801469.561320.13590@z14g2000cwz.googlegroups.com...
> > > my_message = 'Hello World'
> > > my_template =
> > >
File.readlines('my_template_file.txt').HOW-CAN-I-FORCE-INTERPOLATION???
> > > puts my_template <== The message is Hello World
> > >
> > >
> > > Anyone have any ideas? Or am I missing something blatantly obvious?
> >
> > How about:
> >
> > eval "%Q{" + File.read('my_template_file.txt') + "}"
> >
>
> echo "}; 10.times{ puts 'do not trust external data'" > > my_template_file.txt
>
> Cheers :)

Hey, you just spoiled my evil - err - eval plan to take over his system!
:-)

robert

ES

4/6/2005 5:34:00 PM

0


Le 6/4/2005, "Robert Klemme" <bob.news@gmx.net> a écrit:
>"Vraj Mohan" <vrajmohan@comcast.net> schrieb im Newsbeitrag
>news:42540521.7030801@comcast.net...
>> Will using Kernel#sprintf work achieve the same thing?
>> --Vraj Mohan
>>
>
>No.

Problem is that interpolation is done at the time the String
is first encountered, which may complicate things since the
variable used for substitution may not be available, if I am
understanding correctly.

However, this works in those in situations:

# file-one
str = "Inserted %s."

# file-two
require 'file-one'
puts str % 'this from file two'

> robert

E

No-one expects the Solaris POSIX implementation!



John Elrick

4/7/2005 3:16:00 AM

0

Thanks to all who have replied. I appreciate all the solutions.

Best,

John

John Carter

4/7/2005 9:25:00 PM

0