0

This question is very similar to "Ruby array to string conversion", but in my case, and using the table ['12','34','35','231'], I want to get a string like:

'12' &
'32' &
'35' &
'231'

The idea is to construct a string in a decorator method called content, and when I display that specific string in the view <p><%= table.content %></p>, I want it to render automatically as showed, without adding any HTML tags in the view, basically mimicking the <br> tag between each two lines.

What I have already is:

def content
  string = "Table content "
  table = ['12','34','35','231']
  string = table.map do |row|
    row
  end
  "#{string.join(" & \n")}"
end

Any clues?

2
  • 2
    You'll need to wrap the string with a <pre> tag if you want it to display correctly. Commented Aug 22, 2014 at 16:10
  • If you're rending for HTML, then you want something like table.join(" &amp;<br/>").html_safe. Your table.map block just yields an array string that looks just like table. Commented Aug 22, 2014 at 16:44

2 Answers 2

0
def content
  table = ['12','34','35','231']
  results = table.map { |r| "'#{r}'" }.join(" & <br/>")
  "<pre>#{results}</pre>".html_safe
end

map creates a new array by applying each member of the array to the block. This wraps your numbers in single quotes.

A couple of your lines of code were redundant - string = "Table content " was doing nothing and:

string = table.map do |row|
    row
end

was needlessly spinning through the array and returning an identical array.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the answer, but that didn't solve anything! It's just compacting the block of map into one line ! I style have all number in one line in the output...
Just use " &\n" instead of " & \n". No need to add whitespace that the browser will render needlessly.
"It's just compacting the block of map into one line". That's most likely because browsers do that when presented with text inside HTML. That block needs to be wrapped with <pre> or <code>.
<br /> is the best way to go but the OP stated "I want it to render automatically as showed, without adding any HTML tags in the view" also this might require a call to html_safe so that it does not get converted to &lt; and &gt; tags. Maybe try using &#13; => carriage return or &#10; => line feed. Although i have not tried these in html ever as a <br /> tag is the acceptable way to handle this.
Good spot. Then <pre> is the way to go. Otherwise you're just fighting browser behaviour. Why this exact specification?
|
0

It's important to understand how browsers render text.

ary = ['12','34','35','231']

File.write('test.html', '<html><body><pre>' + ary.map{ |s| "'#{ s }'"}.join(" &\n") + '</pre></body></html>')

Which will create an HTML file looking like:

<html><body><pre>'12' &
'34' &
'35' &
'231'</pre></body></html>

Open "test.html" in your browser and you'll see that the output is displayed vertically and look like:

'12' &
'34' &
'35' &
'231'
Browsers gobble whitespace, which means that, unless you wrap your output in [`` tags][1], the browser will ignore line-break characters, and pull the text into a single-line/sentence. They do that because they're being "helpful".

In your code, ary.map{ |s| "'#{ s }'"}.join(" &\n") is what goes in your controller to generate the string. Then, in your view wrap your line that outputs the string with <pre> and you should be good.

2 Comments

Why "helpful"? Think about how helpful it really is. Could you imagine having to type full paragraphs on one line because otherwise they would render with strange breaks. LF and CR ignorance is a helpful benefit for sure is so so so many cases in html.
"Helpful" depends on where you're sitting. The average person browsing the web wants readable pages. They have nothing to do with the average web-page developer who has to generate the HTML that correctly generates the page. And, yes, I can imagine having to type full paragraphs on one line, because that's what most word processors actually do. That they render the text across multiple lines in the display or on the page doesn't change the fact that the paragraph still has a single line-break at the end of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.