Forum: Ruby string substitution question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ljw1001 (Guest)
on 2005-11-28 06:34
(Received via mailing list)
I'm reading a file using:

  " str = IO.read("textfile1.txt") "

and trying to output the results using

   " puts str "
The problem is that I want to substitute values in the file.  It
includes this text: #{(3+4).to_s}

I expected the puts statement to output 7, but it outputs the code
literally as #{(3+4).to_s}

How can i get this substitution to happen?

thanks for your help.
chdiary (Guest)
on 2005-11-28 06:59
(Received via mailing list)
I am not sure, but
how about use eval function

maybe like this
eval(%Q(puts #{str}))

I want it work.
let me know that.
good luck
rosco (Guest)
on 2005-11-28 11:09
(Received via mailing list)
On Mon, 28 Nov 2005 04:28:11 -0000, <removed_email_address@domain.invalid> 
wrote:

> I expected the puts statement to output 7, but it outputs the code
> literally as #{(3+4).to_s}
>
> How can i get this substitution to happen?
>
> thanks for your help.
>

(Caveat: relatively new to Ruby)

I think it's more trouble than it's worth, though I don't doubt it could
be done with some insane escaping and eval. I'd have to guess that the
#{}
expansion is done somewhere in the parser (?).

I recently made use of ERB (included with 1.8.3, don't know about other
versions) for this kind of thing, which worked a treat. Your file would
contain something like:

	<%= 3+4.to_s %>

(P.s. I know it looks a little bit /.*ML/ but it's not specifically -
it's
just the way ERB separates the code.
ljw1001 (Guest)
on 2005-11-28 14:03
(Received via mailing list)
What is ERB?

I had assumed that it would be possible to convert a single quote
string to a double quote string and that would take care of it.  Is
that not possible. Maybe I'm looking at this the wrong way.
Substitution is done with the syntax you mention in my current version
of Rails.  Is ERB what they use?
ljw1001 (Guest)
on 2005-11-28 14:11
(Received via mailing list)
I tried it with my current version 1.8.2 and got this message:

(eval):1: warning: parenthesize argument(s) for future version

I'll try again when I've installed 1.8.3.
rosco (Guest)
on 2005-11-28 23:26
(Received via mailing list)
On Mon, 28 Nov 2005 12:00:37 -0000, <removed_email_address@domain.invalid> 
wrote:

> What is ERB?
>
> I had assumed that it would be possible to convert a single quote
> string to a double quote string and that would take care of it.  Is
> that not possible. Maybe I'm looking at this the wrong way.
> Substitution is done with the syntax you mention in my current version
> of Rails.  Is ERB what they use?
>

It is probably possible, maybe using the special quoting operators and
so
forth, but I imagine it is messy and problematic (?).
There appears to be some automagic escaping going on when # is read into
double quoted strings, or something like that.
Rails does use ERB, and I think I'd still suggest it. It's 'Embedded
Ruby'. With 1.8 it comes as standard (at least with 1.8.3). You can use
it
like:

   require 'erb'

   sturf = ['just', 'some', 'stuff']
   src = "<% sturf.length.times do |i| %><%= sturf[i] %> <% end %>"

   text = ERB.new(src).result       # => "just some stuff "

You can pass a binding to the 'result' method, so that you can do the
following:

   require 'erb'
   class SomeClazz

     def set_some_var
       @foo = 'bar'
       self
     end

     def render_text(s)
       ERB.new(s).result(binding)
     end
   end

   s = SomeClazz.new.set_some_var.render_text('<%= @foo %>')  # => "bar"

There is also a native version of ERB as a command - try 'erb' at your
prompt (I found it's -x option quite helpful when debugging template
problems).
rosco (Guest)
on 2005-11-28 23:30
(Received via mailing list)
On Mon, 28 Nov 2005 21:16:40 -0000, Ross B.
<removed_email_address@domain.invalid> wrote:

> With 1.8 it comes as standard (at least with 1.8.3).

By that I mean 'that I know of' - I don't know exactly which versions
have
ERB included, but I bet it's been in for a while...
w_a_x_man (Guest)
on 2005-11-28 23:50
(Received via mailing list)
Ross B. wrote:
>
>    sturf = ['just', 'some', 'stuff']
>    src = "<% sturf.length.times do |i| %><%= sturf[i] %> <% end %>"

   src = "<% stuff.each do |s| %><%= s %> <% end %>"
rosco (Guest)
on 2005-11-29 00:14
(Received via mailing list)
On Mon, 28 Nov 2005 21:44:34 -0000, William J. 
<removed_email_address@domain.invalid>
wrote:

>>    sturf = ['just', 'some', 'stuff']
>>    src = "<% sturf.length.times do |i| %><%= sturf[i] %> <% end %>"
>
>    src = "<% stuff.each do |s| %><%= s %> <% end %>"
>

Sorry. I was just going for a more esoteric example to illustrate you
can
do anything in there...
ljz (Guest)
on 2005-11-29 03:28
(Received via mailing list)
removed_email_address@domain.invalid writes:

> I tried it with my current version 1.8.2 and got this message:
>
> (eval):1: warning: parenthesize argument(s) for future version
>
> I'll try again when I've installed 1.8.3.

To get rid of that error, all you need to do is this:

  eval(%Q(puts(#{str})))

Note the extra parentheses surrounding #{str}.
This topic is locked and can not be replied to.