erb: undefined local variable

  • Thread starter Tang Hai Tuan Minh
  • Start date
T

Tang Hai Tuan Minh

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
 
B

Brian Candler

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.
 
T

Tang Hai Tuan Minh

Thomas said:
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 :)
 
J

James Edward Gray II

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.

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
 
F

Fritz Heinrichmeyer

Brian said:
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 ...
 
J

James Edward Gray II

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top