[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

A problem about replacing a string in a template.

Kuang Dong

12/25/2006 8:20:00 AM

File 1: test.tpl

Hello,#{name}!

File 2: test.rb

name = "jack"
f = File.open("test.tpl")
puts f.read
puts "Hello,#{name}"
f.close

And the result is:
Hello,#{name}!
Hello,jack!

How to change the "#{name}" to "jack"?

--
Posted via http://www.ruby-....

11 Answers

Gerardo Santana Gómez Garrido

12/25/2006 10:47:00 AM

0

2006/12/25, Kuang Dong <kuangdong@gmail.com>:
> File 1: test.tpl
>
> Hello,#{name}!
>
> File 2: test.rb
>
> name = "jack"
> f = File.open("test.tpl")
> puts f.read
> puts "Hello,#{name}"
> f.close
>
> And the result is:
> Hello,#{name}!
> Hello,jack!
>
> How to change the "#{name}" to "jack"?

puts eval f.read


--
Gerardo Santana

Andrea Fazzi

12/25/2006 10:48:00 AM

0

Kuang Dong wrote:
> File 1: test.tpl
>
> Hello,#{name}!
>
> File 2: test.rb
>
> name = "jack"
> f = File.open("test.tpl")
> puts f.read
> puts "Hello,#{name}"
> f.close
>
> And the result is:
> Hello,#{name}!
> Hello,jack!
>
> How to change the "#{name}" to "jack"?
>
>



Use ERB to do templating!


File 1: test.tpl

Hello, <%= name %>!

File 2: test.rb

require 'erb'

name = "Jack"
File.open('test.tpl') { |file| puts ERB.new(file.read).result }

Bye.
Andrea


Morton Goldberg

12/25/2006 11:00:00 AM

0

On Dec 25, 2006, at 3:20 AM, Kuang Dong wrote:

> File 1: test.tpl
>
> Hello,#{name}!
>
> File 2: test.rb
>
> name = "jack"
> f = File.open("test.tpl")
> puts f.read
> puts "Hello,#{name}"
> f.close
>
> And the result is:
> Hello,#{name}!
> Hello,jack!
>
> How to change the "#{name}" to "jack"?

AFAIK, the Ruby #{...} substitution facility only works when a string
literal is evaluated and converted into a String object. Therefore,
to do what you want using #{...} substitution, you will need to build
a double-quoted string from the template file contents and then
evaluate that string. For example:

<code>
File.open('/tmp/test.tpl', 'w') do |f|
f.write 'Hello, #{name}!'
end

name = "Jack"
File.open("/tmp/test.tpl") do |f|
puts eval('"' + f.read + '"')
end
</code>

However, you might also consider a different approach. Define your
own template format and use String#gsub or String#gsub! to do the
replacement.

<code>
File.open('/tmp/test.tpl', 'w') do |f|
f.write 'Hello, #name#!'
end

File.open("/tmp/test.tpl") do |f|
puts f.read.gsub('#name#', 'Jack')
end
</code>

In the above example, I could have used String#sub instead of gusb
since there was only one substitution to be made.

Regards, Morton

Gavin Kistner

12/25/2006 7:35:00 PM

0

Andrea Fazzi wrote:
> Use ERB to do templating!
> File 2: test.rb
>
> require 'erb'
>
> name = "Jack"
> File.open('test.tpl') { |file| puts ERB.new(file.read).result }

The above won't quite work; you need to pass in the binding to use
local variables from the current scope.

Here's one that does work, and slightly shorter, to boot:

File1: hello.tpl
Hello <%=name%>

File2: test.rb
require 'erb'
name = "Jack"
puts ERB.new( IO.read( 'hello.tpl' ) ).result( binding )

Andrea Fazzi

12/25/2006 8:08:00 PM

0

Phrogz wrote:
> Andrea Fazzi wrote:
>
>> Use ERB to do templating!
>> File 2: test.rb
>>
>> require 'erb'
>>
>> name = "Jack"
>> File.open('test.tpl') { |file| puts ERB.new(file.read).result }
>>
>
> The above won't quite work; you need to pass in the binding to use
> local variables from the current scope.
>

It works, please test it. In fact, If you don't pass explicity the
binding argument, then the TOPLEVEL_BINDING is passed by default.

Andrea

--
Andrea Fazzi @ Alca Societa' Cooperativa
Servizi di Informatica Libera

Lecce - Italy
http://a...



Gavin Kistner

12/25/2006 8:22:00 PM

0

Andrea Fazzi wrote:
> Phrogz wrote:
> It works, please test it. In fact, If you don't pass explicity the
> binding argument, then the TOPLEVEL_BINDING is passed by default.

Hrm, I did test it before I posted, albeit not exactly the code you
pasted. Here's what I tested:

template = "Hello <%=name%>"
name = "Andrea"

require 'erb'
puts ERB.new( template ).result
#=> NameError: undefined local variable or method `name' for
main:Object

puts ERB.new( template ).result( binding )
#=> "Hello Andrea"

Am I mistaken? Is this substantively different from what you wrote, and
I'm missing something?

Gavin Kistner

12/25/2006 8:26:00 PM

0

Phrogz wrote:
> Andrea Fazzi wrote:
> > It works, please test it. In fact, If you don't pass explicity the
> > binding argument, then the TOPLEVEL_BINDING is passed by default.
>
> Hrm, I did test it before I posted, albeit not exactly the code you
> pasted.

I just tested your original code exactly, and it gave me the same
error. I'm using:
[sliver:~] gkistner$ ruby -v
ruby 1.8.5 (2006-08-25) [powerpc-darwin8.7.0]

What version of Ruby are you using that's working for you?

Andrea Fazzi

12/25/2006 8:39:00 PM

0

Phrogz wrote:
> Phrogz wrote:
>
>> Andrea Fazzi wrote:
>>
>>> It works, please test it. In fact, If you don't pass explicity the
>>> binding argument, then the TOPLEVEL_BINDING is passed by default.
>>>
>> Hrm, I did test it before I posted, albeit not exactly the code you
>> pasted.
>>
>
> I just tested your original code exactly, and it gave me the same
> error. I'm using:
> [sliver:~] gkistner$ ruby -v
> ruby 1.8.5 (2006-08-25) [powerpc-darwin8.7.0]
>
> What version of Ruby are you using that's working for you?
>
>
>

I'm using:

andrea@ganimede:~$ ruby -v
ruby 1.8.4 (2005-12-24) [powerpc-linux]

Hmm.. why it works for me?? :-)

--
Andrea Fazzi @ Alca Societa' Cooperativa
Servizi di Informatica Libera

Lecce - Italy
http://a...



James Gray

12/25/2006 11:10:00 PM

0

On Dec 25, 2006, at 2:39 PM, Andrea Fazzi wrote:

> Phrogz wrote:
>> Phrogz wrote:
>>
>>> Andrea Fazzi wrote:
>>>
>>>> It works, please test it. In fact, If you don't pass explicity the
>>>> binding argument, then the TOPLEVEL_BINDING is passed by default.
>>>>
>>> Hrm, I did test it before I posted, albeit not exactly the code you
>>> pasted.
>>>
>>
>> I just tested your original code exactly, and it gave me the same
>> error. I'm using:
>> [sliver:~] gkistner$ ruby -v
>> ruby 1.8.5 (2006-08-25) [powerpc-darwin8.7.0]
>>
>> What version of Ruby are you using that's working for you?
>>
>>
>>
>
> I'm using:
>
> andrea@ganimede:~$ ruby -v
> ruby 1.8.4 (2005-12-24) [powerpc-linux]
>
> Hmm.. why it works for me?? :-)

I'm pretty sure ERb was changed in this recently.

James Edward Gray II

Andrea Fazzi

12/26/2006 8:35:00 AM

0

James Edward Gray II wrote:
>
> I'm pretty sure ERb was changed in this recently.
>
> James Edward Gray II
>

Ok, this is the erb's version I'm using:

andrea@ganimede:~$ erb1.8 --version
erb.rb [2.0.4 2005/02/12]

Which version of erb are you using Phrogz?

--
Andrea Fazzi @ Alca Societa' Cooperativa
Servizi di Informatica Libera

Lecce - Italy
http://a...