How to Create Line Breaks in Ruby

How do i create line breaks in ruby?

Use puts since it will automatically add a newline for you:

puts "Hi"
puts "Hi"

If you want to make an explicit newline character then you'll need to know what kind of system(s) on which your program will run:

print "Hi\n"   # For UNIX-like systems including Mac OS X.
print "Hi\r\n" # For Windows.

line break for a string placeholder in ruby

The newline character \n should be included between the double, however HTML does not allow for line feed, but Thomas Hunter suggested an hack which consists in using a bunch of white spaces, like so:

<%= f.text_area :comment, placeholder: "Comment on your track or                  share your favorite lyrics" %>

You can also opt to use the title attribute instead.

Given a string, how to append a carriage return followed by another string?

It really depends on what you are outputting to.

$STDOUT:

puts "Hello\n\n#{myURL}"

or

puts "Hello"
puts
puts myURL

or

puts <<EOF
Hello

#{myURL}
EOF

If you are outputting this in an html.erb or .rhtml document:

<%= "Hello<br /><br />#{myURL}" %> # or link_to helper

If you already have a string like string1 then you can append to it using either += or <<:

string1  = "Hello world, join my game:"
myUrl = "http://example.com"
string1 += "\n\n#{myUrl}"

or:

string1 = "Hello world, join my game:"
myUrl = "http://example.com"
string +=<<EOF

#{myUrl}
Here's some other details
EOF

How to do a newline in output

Use "\n" instead of '\n'

Line breaks in Slim Text

I found a solution but not a pretty one...

| 
Vous pouvez remonter votre offre en tête de liste afin de :
* Booster la visibilité de votre offre
* Bénéficier de 15 jours supplémentaires de diffusion

= '\n\n' + @job_offer_publish_url

I'll be happy to have something better !

How to use line breaks in a slim variable?

There are two issues here. First in Ruby strings using single quotes – ' – don’t convert \n to newlines, they remain as literal \ and n. You need to use double quotes. This applies to Slim too.

Second, Slim HTML escapes the result of interpolation by default. To avoid this use double braces around the code. Slim also HTML escapes Ruby output by default (using =). To avoid escaping in that case use double equals (==).

Combining these two, your code will look something like:

- foo = "my \n desired multiline <br/> string"
td #{{foo}}

This produces:

<td>my
desired multiline <br/> string</td>

How to insert new line breaks using form_for with collection_check_boxes

from reference here
It is possibly to customize the way the elements will be shown by giving a block to the method as sample below from your code above

<%= form_for @user do |f| %> 
<%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, :topic_name do |b| %>
<%= b.label(class: "check_box") do %>
<%= b.check_box(class: "check_box") %>
<%= b.object.topic_name %></br>
<% end %>
<% end %>
<%= f.submit %>
<% end %>

Line Break for a Ruby Array

You are almost there, instead of outputing a to_a version of your array you should iterate through the each_slice items and print the results.

<p>[</p>
<% @register_number.shuffle.each_slice(2) do |duet| %>
<p> [<%= duet.join(', ') %>] </p>
<% end %>
<p>]</p>


Related Topics



Leave a reply



Submit