Erb example from comp.lang.ruby fails

I copied code from Ruby Standard Library Documentation (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

On Mon, Feb 28, 2011 at 8:00 PM, RichardOnRails
[email protected] wrote:

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.

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

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.

On Mon, Feb 28, 2011 at 7:00 PM, RichardOnRails
[email protected] wrote:

from K:/_Utilities/ruby186-26_rc2/ruby/lib/ruby/1.8/erb.rb:741:in
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

On Feb 28, 2:20pm, Phillip G. [email protected]
wrote:

Running the code from within SciTE 1.74 gave me the error message:
If there’s a easily recognized fix, I’d like to apply it to this

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

On Feb 28, 6:01pm, RichardOnRails
[email protected] wrote:

% end

published a new version, with a copy of printed output appended,
athttp://www.pastie.org/1617782
“*” out there would lead to:
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

On Tue, Mar 1, 2011 at 12:20 AM, RichardOnRails
[email protected] wrote:

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.

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| >
<%= " * #{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. :slight_smile:


Phillip G.

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.

On Feb 28, 2:38pm, Sean O’Halpin [email protected] wrote:

Running the code from within SciTE 1.74 gave me the error message:
If there’s a easily recognized fix, I’d like to apply it to this
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

On Tue, Mar 1, 2011 at 12:05 AM, RichardOnRails
[email protected] wrote:

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?

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 starting with <% and ending in %>
  >  omit newline for lines ending in %>

Regards,
Sean