Ruby in html.erb files

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!

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

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!

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

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

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.

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!