erb example from comp.lang.ruby fails

R

RichardOnRails

I copied code from http://www.ruby-doc.org/stdlib-1.8.6/ (erb package
in Table of Contents, ERB in Classes column.

I edited it only slightly in SciTE and pasted it in http://www.pastie.org/1617782

Running the code from within SciTE 1.74 gave me the error message:
(erb):16: undefined local variable or method `priority' for
main:Object (NameError)
from K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/1.8/erb.rb:741:in
`value'
from K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/1.8/erb.rb:741:in
`result'
from erb1.rb:43

I'm running ruby 1.8.6 under WinXP-Pro/SP3

If there's a easily recognized fix, I'd like to apply it to this
example. Meanwhile, I'll try other examples that might both work and
suggest a hint at why this example fails.

Thanks in Advance,
Richard
 
P

Phillip Gawlowski

I copied code from http://www.ruby-doc.org/stdlib-1.8.6/ (erb package
in Table of Contents, ERB in Classes column.

I edited it only slightly in SciTE and pasted it in http://www.pastie.org= /1617782

Running the code from within SciTE 1.74 gave me the error message:
(erb):16: undefined local variable or method `priority' for
main:Object (NameError)
=A0 =A0 =A0 =A0from K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/1.8/erb.rb= :741:in
`value'
=A0 =A0 =A0 =A0from K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/1.8/erb.rb= :741:in
`result'
=A0 =A0 =A0 =A0from erb1.rb:43

I'm running ruby 1.8.6 under WinXP-Pro/SP3

If there's a easily recognized fix, =A0I'd like to apply it to this
example. =A0Meanwhile, =A0I'll try other examples that might both work an= d
suggest a hint at why this example fails.

This seems to be the offending code:
<%# ignore numerous minor requests -- focus on priorities %>
% priorities.each do |priority|
* <%=3D priority %>
% end

Try this instead:

<%# ignore numerous minor requests -- focus on priorities %>
<% priorities.each do |priority| >
* <%=3D priority %>
< % end >


--=20
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.
 
S

Sean O'Halpin

I copied code from http://www.ruby-doc.org/stdlib-1.8.6/ (erb package
in Table of Contents, ERB in Classes column.

I edited it only slightly in SciTE and pasted it in http://www.pastie.org= /1617782

Running the code from within SciTE 1.74 gave me the error message:
(erb):16: undefined local variable or method `priority' for
main:Object (NameError)
=A0 =A0 =A0 =A0from K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/1.8/erb.rb= :741:in
`value'
=A0 =A0 =A0 =A0from K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/1.8/erb.rb= :741:in
`result'
=A0 =A0 =A0 =A0from erb1.rb:43

I'm running ruby 1.8.6 under WinXP-Pro/SP3

If there's a easily recognized fix, =A0I'd like to apply it to this
example. =A0Meanwhile, =A0I'll try other examples that might both work an= d
suggest a hint at why this example fails.

Thanks in Advance,
Richard

That ".gsub(/^ /, '')" at the end of the template is the clue. It
deletes the first two spaces at the beginning of each line.
The '%' modifier works only at the very beginning of each line. If you
change either the indent of the template to two spaces
or change the gsub to replace the same number of spaces you've
indented by, the example will work.

Regards,
Sean
 
R

RichardOnRails

This seems to be the offending code:
<%# ignore numerous minor requests -- focus on priorities %>
    % priorities.each do |priority|
      * <%= priority %>
    % end

Try this instead:

<%# ignore numerous minor requests -- focus on priorities %>
    <% priorities.each do |priority| >
      * <%= priority %>
    < % end >

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.

Hi Phillip,

Thanks for you response, which essentially solved my problem. I
published a new version, with a copy of printed output appended, at
http://www.pastie.org/1617782
Try this instead:

<%# ignore numerous minor requests -- focus on priorities %>
<% priorities.each do |priority| >
* <%= priority %>
< % end >

After following your suggestion and getting different failure, I
finally realized: Hey, that's just regular ERB code. Therefore, that
"*" out there would lead to:
string * string * string
which violates Ruby syntax. So I got rid of the "*" and got decent
(but not perfect results): Too many blank lines are generated.

I eliminated one of the blanks by appending a closing hyphen at line
24. But I couldn't plug the major generated, I believe, by line 26.
Cf. output lines 70-72, 74,76,78 in the Pastie. If you have have any
insight into that problem, I'd appreciate one more insight from you.

Best wishes,
Richard
 
R

RichardOnRails

Hi Phillip,

Thanks for you response, which essentially solved my problem. I
published a new version, with a copy of printed output appended, athttp://www.pastie.org/1617782



After following your suggestion and getting different failure, I
finally realized:  Hey, that's just regular ERB code. Therefore,  that
"*" out there would lead to:
    string * string * string
which violates Ruby syntax.  So I got rid of the "*" and got decent
(but not perfect results):  Too many blank lines are generated.

I eliminated one of the blanks by appending a closing hyphen at line
24.  But I couldn't plug the major generated, I believe, by line 26.
Cf. output lines 70-72, 74,76,78 in the Pastie.  If you have have any
insight into that problem,  I'd appreciate one more insight from you.

Best wishes,
Richard

Phillip,

When I pasted my corrected version, Pastie gave me a new URL:
http://www.pastie.org/1617782
I appologize for the mistake.

Best,
Richard
 
P

Phillip Gawlowski

Alas, I don't. It seems to be a quirk of ERb, or at least I haven't
found anything to change that behaviour, unfortunately.

Also, if you want bullet points, you can try something like this:

<%# ignore numerous minor requests -- focus on priorities %>
<% priorities.each do |priority| >
<%=3D " * #{priority}" %>
< % end >

Basically, this creates a string containing both the splat and the
string from the priority Array.
When I pasted my corrected version, Pastie gave me a new URL:
http://www.pastie.org/1617782
I appologize for the mistake.

None needed. :)

--=20
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.
 
R

RichardOnRails

That ".gsub(/^  /, '')" at the end of the template is the clue. It
deletes the first two spaces at the beginning of each line.
The '%' modifier works only at the very beginning of each line. If you
change either the indent of the template to two spaces
or change the gsub to replace the same number of spaces you've
indented by, the example will work.

Regards,
Sean

Hi Sean (namesake of my 6-yo grandson),

Great job. I posted the corrected version, including appended output,
at http://www.pastie.org/1618793.

I recognized how the gsub would affect the earlier code, but saw no
purpose for it ... because I never heard about a % sign in the first
column producing some special effect. Do you know where that's
documented online?
The Pastie proves that the code produces excellent output.

Many thanks,
Richard
 
S

Sean O'Halpin

I recognized how the gsub would affect the earlier code, but saw no
purpose for it ... because I never heard about a % sign in the first
column producing some special effect. Do you know where that's
documented online?

http://www.ruby-doc.org/stdlib-1.8.6/libdoc/erb/rdoc/classes/ERB.html#M000650

See the docs for the parameter 'trim_mode':

If trim_mode is passed a String containing one or more of the
following modifiers, ERB will adjust its code generation as listed:

% enables Ruby code processing for lines beginning with %
omit newline for lines ending in %>

Regards,
Sean
 

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

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top