# Ruby in html.erb files

**URL:** <https://www.ruby-forum.com/t/ruby-in-html-erb-files/141579>\
**Category:** Rails\
**Created:** [June 27, 2008, 9:41am UTC](https://www.ruby-forum.com/t/ruby-in-html-erb-files/141579 "2008-06-27T09:41:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amrita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@amrita](https://www.ruby-forum.com/u/amrita)\
**Post date:** [June 27, 2008, 9:41am UTC](https://www.ruby-forum.com/t/ruby-in-html-erb-files/141579/1 "2008-06-27T09:41:22Z")

</div>

I have just started learning Rails and Ruby. I have succeeded in  
building an application so far. However, recently I tried embedding  
some ruby code in one of my html.erb files with the intention of  
further formatting the string that was being output. The original  
string was a set of lines with tab-delimited fields. So, I wanted to  
put it in pretty print HTML. For this, i used some string functions  
such as String.lines. When I try to view the said page, I get the  
following error:

undefined method `lines’ for #String:0x240d448

The line of code where the error appears is:

@longshortgene.parsed\_blast\_report.lines { |line| print @line }

And if I insert a require ‘String’ in the beginning of the file, it  
still gives me the following error:

no such file to load – String

I am completely new to Ruby, although I am learning the language  
parallel with Rails.

Can somebody please help me understand this problem, or point me to a  
relevant resource?

Thanks!

---

<div class="post-metadata">

**Author:** ![amrita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@amrita](https://www.ruby-forum.com/u/amrita)\
**Post date:** [June 27, 2008, 9:47am UTC](https://www.ruby-forum.com/t/ruby-in-html-erb-files/141579/2 "2008-06-27T09:47:14Z")

</div>

On Jun 26, 11:14 pm, amrita [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> undefined method `lines’ for #String:0x240d448
> 
> The line of code where the error appears is:
> 
> @longshortgene.parsed\_blast\_report.lines { |line| print @line }

What is @longshortgene.parsed\_blast\_report ? Did you mean each\_line ?  
Also the variable you’re using inside the block should be line, not  
@line (it’s got to match what is in between the | |)  
You should also parse the result through h or you may output invalid  
html

Fred

---

<div class="post-metadata">

**Author:** ![amrita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@amrita](https://www.ruby-forum.com/u/amrita)\
**Post date:** [June 27, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/ruby-in-html-erb-files/141579/3 "2008-06-27T19:16:28Z")

</div>

Thanks, Fred. @longshortgene.parsed\_blast\_report is a string, which is  
a set of lines, each line consisting of tab-delimited fields. When I  
print @longshortgene.parsed\_blast\_report, it prints correctly. It is  
only when I try to parse it that I get the “undefined method” error  
for the method “lines”. Do I need to create a new String object first  
using @longshortgene.parsed\_blast\_report and then use the method lines  
on it? I am going to try that next.

I corrected ‘@line’ to ‘line’ and the error still persists.

Thanks!

On Jun 27, 12:46 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![amrita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@amrita](https://www.ruby-forum.com/u/amrita)\
**Post date:** [June 27, 2008, 8:08pm UTC](https://www.ruby-forum.com/t/ruby-in-html-erb-files/141579/4 "2008-06-27T20:08:51Z")

</div>

On 27 Jun 2008, at 18:15, amrita wrote:

> Thanks, Fred. @longshortgene.parsed\_blast\_report is a string, which is  
> a set of lines, each line consisting of tab-delimited fields. When I  
> print @longshortgene.parsed\_blast\_report, it prints correctly. It is  
> only when I try to parse it that I get the “undefined method” error  
> for the method “lines”. Do I need to create a new String object first  
> using @longshortgene.parsed\_blast\_report and then use the method lines  
> on it? I am going to try that next.

String doesn’t have a lines method before ruby 1.8.7.

Fred

---

<div class="post-metadata">

**Author:** ![amrita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@amrita](https://www.ruby-forum.com/u/amrita)\
**Post date:** [June 27, 2008, 8:08pm UTC](https://www.ruby-forum.com/t/ruby-in-html-erb-files/141579/5 "2008-06-27T20:08:16Z")

</div>

I just checked this.  
There is in fact a method lines mentioned in the Ruby API docs for  
string.  
But if you list the methods of a string, it’s missing.

that’s from the docs:

str.lines(separator=$/) =\> anEnumerator  
str.lines(separator=$/) {|substr| block } =\> str

Returns an enumerator that gives each line in the string. If a block  
is given, it iterates over each line in the string.

“foo\nbar\n”.lines.to\_a #=\> [“foo\n”, “bar\n”]  
“foo\nb ar”.lines.sort #=\> [“b ar”, “foo\n”]

So I see, what you want to do, but I don’t know, what happened to this  
method.  
Maybe you can use one of te other string functions like split

---

<div class="post-metadata">

**Author:** ![amrita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@amrita](https://www.ruby-forum.com/u/amrita)\
**Post date:** [June 27, 2008, 8:38pm UTC](https://www.ruby-forum.com/t/ruby-in-html-erb-files/141579/6 "2008-06-27T20:38:10Z")

</div>

OK, now that makes sense for the error to show up then. Let me try  
other methods such as split. Thank you, Fred and Thorsten, for your  
help.

---

<div class="post-metadata">

**Author:** ![amrita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@amrita](https://www.ruby-forum.com/u/amrita)\
**Post date:** [June 27, 2008, 11:52pm UTC](https://www.ruby-forum.com/t/ruby-in-html-erb-files/141579/7 "2008-06-27T23:52:33Z")

</div>

OK, here is the exact Ruby code I use now.

Line1: \<%= @longshortgene.parsed\_blast\_report %\>  
Line2: \<% @longshortgene.parsed\_blast\_report.split(/\n/).each do |  
field| %\>  
Line3: \<%= field %\>  
Line4: \<% end %\>

It works! Thanks again!
