# Erb example from comp.lang.ruby fails

**URL:** <https://rubytalk.org/t/erb-example-from-comp-lang-ruby-fails/61982>\
**Category:** ruby-talk\
**Created:** [28 February 2011 19:00 UTC](https://rubytalk.org/t/erb-example-from-comp-lang-ruby-fails/61982 "2011-02-28T19:00:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard3](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@Richard3](https://rubytalk.org/u/Richard3)\
**Post date:** [28 February 2011 19:00 UTC](https://rubytalk.org/t/erb-example-from-comp-lang-ruby-fails/61982/1 "2011-02-28T19:00:33Z")

</div>

I copied code from [http://www.ruby-doc.org/stdlib-1.8.6/](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](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)  
&nbsp;&nbsp;from K:/\_Utilities/ruby186-26\_rc2/ruby/lib/ruby/1.8/erb.rb:741:in  
`value'  
&nbsp;&nbsp;from K:/\_Utilities/ruby186-26\_rc2/ruby/lib/ruby/1.8/erb.rb:741:in  
`result'  
&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)\
**Post date:** [28 February 2011 19:20 UTC](https://rubytalk.org/t/erb-example-from-comp-lang-ruby-fails/61982/2 "2011-02-28T19:20:44Z")

</div>

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

Try this instead:

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

> **···**
>
> On Mon, Feb 28, 2011 at 8:00 PM, RichardOnRails \<RichardDummyMailbox58407@uscomputergurus.com\> wrote:
> 
> > I copied code from [Ruby Standard Library Documentation](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](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.
> 
> --  
> 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.

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [28 February 2011 19:38 UTC](https://rubytalk.org/t/erb-example-from-comp-lang-ruby-fails/61982/3 "2011-02-28T19:38:57Z")

</div>

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 Mon, Feb 28, 2011 at 7:00 PM, RichardOnRails \<RichardDummyMailbox58407@uscomputergurus.com\> wrote:
> 
> > I copied code from [Ruby Standard Library Documentation](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](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

---

<div class="post-metadata">

**Author:** ![Richard3](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@Richard3](https://rubytalk.org/u/Richard3)\
**Post date:** [28 February 2011 23:05 UTC](https://rubytalk.org/t/erb-example-from-comp-lang-ruby-fails/61982/4 "2011-02-28T23:05:25Z")

</div>

> \> I copied code fromhttp://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 inhttp://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.
> 
> 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](http://www.pastie.org/1617782)

> Try this instead:
> 
> \<%# ignore numerous minor requests -- focus on priorities %\>  
> &nbsp;&nbsp;&nbsp;&nbsp;\<% priorities.each do |priority| \>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* \<%= priority %\>  
> &nbsp;&nbsp;&nbsp;&nbsp;\< % 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:  
&nbsp;&nbsp;&nbsp;&nbsp;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, 2:20 pm, Phillip Gawlowski \<cmdjackr...@googlemail.com\> wrote:
> 
> > On Mon, Feb 28, 2011 at 8:00 PM, RichardOnRails \> \> \> \> \<RichardDummyMailbox58...@uscomputergurus.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Richard3](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@Richard3](https://rubytalk.org/u/Richard3)\
**Post date:** [1 March 2011 00:05 UTC](https://rubytalk.org/t/erb-example-from-comp-lang-ruby-fails/61982/5 "2011-03-01T00:05:30Z")

</div>

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

Great job. I posted the corrected version, including appended output,  
at [http://www.pastie.org/1618793\](http://www.pastie.org/1618793%5C).

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 Feb 28, 2:38 pm, Sean O'Halpin \<sean.ohal...@gmail.com\> wrote:
> 
> > On Mon, Feb 28, 2011 at 7:00 PM, RichardOnRails \> \> \> \> \<RichardDummyMailbox58...@uscomputergurus.com\> wrote:  
> > \> I copied code fromhttp://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 inhttp://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
> > 
> > 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

---

<div class="post-metadata">

**Author:** ![Richard3](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@Richard3](https://rubytalk.org/u/Richard3)\
**Post date:** [28 February 2011 23:20 UTC](https://rubytalk.org/t/erb-example-from-comp-lang-ruby-fails/61982/6 "2011-02-28T23:20:24Z")

</div>

Phillip,

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

Best,  
Richard

> **···**
>
> On Feb 28, 6:01 pm, RichardOnRails \<RichardDummyMailbox58...@USComputerGurus.com\> wrote:
> 
> > On Feb 28, 2:20 pm, Phillip Gawlowski \<cmdjackr...@googlemail.com\> \> wrote:
> > 
> > \> On Mon, Feb 28, 2011 at 8:00 PM, RichardOnRails \> \> \> \<RichardDummyMailbox58...@uscomputergurus.com\> wrote:  
> > \> \> I copied code fromhttp://www.ruby-doc.org/stdlib-1.8.6/(erbpackage  
> > \> \> in Table of Contents, ERB in Classes column.
> > 
> > \> \> I edited it only slightly in SciTE and pasted it inhttp://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.
> > 
> > \> 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, athttp://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

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [1 March 2011 08:22 UTC](https://rubytalk.org/t/erb-example-from-comp-lang-ruby-fails/61982/7 "2011-03-01T08:22:37Z")

</div>

> 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?

> **[Class: ERB (Ruby 1.8.6)](https://ruby-doc.org/stdlib-1.8.6/libdoc/erb/rdoc/ERB.html)**
>
> Class : ERB - Ruby 1.8.6

See the docs for the parameter 'trim\_mode':

&nbsp;&nbsp;If trim\_mode is passed a String containing one or more of the  
following modifiers, ERB will adjust its code generation as listed:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;% enables Ruby code processing for lines beginning with %  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<\> omit newline for lines starting with \<% and ending in %\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> omit newline for lines ending in %\>

Regards,  
Sean

> **···**
>
> On Tue, Mar 1, 2011 at 12:05 AM, RichardOnRails \<RichardDummyMailbox58407@uscomputergurus.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)\
**Post date:** [28 February 2011 23:29 UTC](https://rubytalk.org/t/erb-example-from-comp-lang-ruby-fails/61982/8 "2011-02-28T23:29:05Z")

</div>

> > 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 %\>  
&nbsp;&nbsp;&nbsp;\<% priorities.each do |priority| \>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= " \* #{priority}" %\>  
&nbsp;&nbsp;&nbsp;\< % 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](http://www.pastie.org/1617782)  
> I appologize for the mistake.

None needed. 🙂

> **···**
>
> On Tue, Mar 1, 2011 at 12:20 AM, RichardOnRails \<RichardDummyMailbox58407@uscomputergurus.com\> wrote:
> 
> --  
> 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.
