Erb: undefined local variable

Hi all,

This is my first time into the world of Ruby. I was interested in erb and so
I copied part of a code fragment from the PickAxe book (2nd Ed.) into a
file called sample.html, ran erb over it (erb sample.html), and received
the error message
"(erb): undefined local variable or method `i' for main:Object(NameError)"

The file sample.html is as follows

<html>
    <head>
        <title> Eruby Example </title>
    </head>
    <body>
        <h1> Enumeration </h1>
        <ul>
            %5.times do |i|
            <li><%=i%></li>
            %end
        </ul>
    </body>
</html>

Now, if I ran erb with erb << ENDRB and write the above code segment before
ENDRB then everything is fine. I had read in the PickAxe book that this is
also similar to what you might get by using irb instead of the ruby
intepreter. However, the solution of sandwiching the code block between
begin and end doesn't help in my case.

Can anyone please enlighten me to a solution ? It's kinda late here so I am
too lazy too think.

TIA,
Minh

···

--
Three minutes' thought would suffice to find this out; but thought is
irksome and three minutes is a long time.
     -- A.E. Houseman

Hello

May just have been the way it arrived in my email message, but ruby code in erb has to be surrounded by <% and %>.

So your sample would need to be:
<html>
     <head>
         <title> Eruby Example </title>
     </head>
     <body>
         <h1> Enumeration </h1>
         <ul>
            <% 5.times do |i| %>
             <li><%=i%></li>
             <% end %>
         </ul>
     </body>
</html>

Tom

···

On 1 Nov 2004, at 09:08, Tang Hai Tuan Minh wrote:

Hi all,

This is my first time into the world of Ruby. I was interested in erb and so
I copied part of a code fragment from the PickAxe book (2nd Ed.) into a
file called sample.html, ran erb over it (erb sample.html), and received
the error message
"(erb): undefined local variable or method `i' for main:Object(NameError)"

The file sample.html is as follows

<html>
    <head>
        <title> Eruby Example </title>
    </head>
    <body>
        <h1> Enumeration </h1>
        <ul>
            %5.times do |i|
            <li><%=i%></li>
            %end
        </ul>
    </body>
</html>

Now, if I ran erb with erb << ENDRB and write the above code segment before
ENDRB then everything is fine. I had read in the PickAxe book that this is
also similar to what you might get by using irb instead of the ruby
intepreter. However, the solution of sandwiching the code block between
begin and end doesn't help in my case.

Can anyone please enlighten me to a solution ? It's kinda late here so I am
too lazy too think.

TIA,
Minh

--
Three minutes' thought would suffice to find this out; but thought is
irksome and three minutes is a long time.
     -- A.E. Houseman

This is my first time into the world of Ruby. I was interested in erb and so
I copied part of a code fragment from the PickAxe book

Unfortunately, Amazon haven't dispatched mine yet, so I can't see exactly the
example you are copying.

erb suffers from poor documentation. One way to make your example work is
by using a different syntax:

<html>
    <head>
        <title> Eruby Example </title>
    </head>
    <body>
        <h1> Enumeration </h1>
        <ul>
            <% 5.times do |i| %>
              <li><%=i%></li>
            <% end %>
        </ul>
    </body>
</html>

However, the source of erb.rb is there to be hacked... poking around, it
looks like you must put the percent as the *first* character of the line. So
your code works if you remove the spaces at the front of those lines:

<html>
    <head>
        <title> Eruby Example </title>
    </head>
    <body>
        <h1> Enumeration </h1>
        <ul>
% 5.times do |i|
              <li><%=i%></li>
% end
        </ul>
    </body>
</html>

Whether it's supposed to be possible to have the percent further indented, I
cannot tell. The erb.rb code is completely uncommented; there are a bunch of
things like 'trim modes' but I cannot work out what they are supposed to do.

Regards,

Brian.

Thomas Counsell wrote:

Hello

May just have been the way it arrived in my email message, but ruby
code in erb has to be surrounded by <% and %>.

So your sample would need to be:
<html>
     <head>
         <title> Eruby Example </title>
     </head>
     <body>
         <h1> Enumeration </h1>
         <ul>
            <% 5.times do |i| %>
             <li><%=i%></li>
             <% end %>
         </ul>
     </body>
</html>

Tom

Yup, that did the trick... And now, after I reread the relevant page in the
PickAxe book (specifically, the page mentioning )

% line of code
with the description that
"A line that starts with a percent is assumed to contain just Ruby Code"

It turns out that vim autoformat my file by adding some additional tabs and
so the original code block had tabs before % 5.times do |i| which I believe
caused erb to skip it, thereby giving rise to the undefined local variable
error message.

Hmm... which means that it's much safer to just use the <% Ruby Code %> form
from now on :slight_smile:

···

--
Three minutes' thought would suffice to find this out; but thought is
irksome and three minutes is a long time.
-- A.E. Houseman

Just FYI, I am currently documenting the erb module. I've been asking questions here and working with Gavin Sinclair behind the scenes. I'm getting pretty close now, so it should be available soon...

James Edward Gray I

···

On Nov 1, 2004, at 3:56 AM, Brian Candler wrote:

Whether it's supposed to be possible to have the percent further indented, I cannot tell. The erb.rb code is completely uncommented; there are a bunch of things like 'trim modes' but I cannot work out what they are supposed to do.

Brian Candler schrieb:

This is my first time into the world of Ruby. I was interested in erb and so
I copied part of a code fragment from the PickAxe book

Unfortunately, Amazon haven't dispatched mine yet, so I can't see exactly the
example you are copying.

erb suffers from poor documentation. One way to make your example work is
by using a different syntax:

the % ruby-line syntax is unfortunate IMO, it prevents using ERB with latex-files ...

···

--
Mit freundlichen Gruessen
Fritz Heinrichmeyer FernUniversitaet, LG ES, 58084 Hagen (Germany)
tel:+49 2331/987-1166 fax:987-355

That's an option (off by default) in the ERB class. The stand-alone "erb" has it on by default, but allows it to be disabled with the -P flag.

James Edward Gray II

···

On Nov 2, 2004, at 5:23 AM, Fritz Heinrichmeyer wrote:

the % ruby-line syntax is unfortunate IMO, it prevents using ERB with latex-files