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}"
orputs "Hello"
puts
puts myURL
orputs <<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
After_Save Callback to Set The Updated_By Column to The Current_User
Gem Install Pg for Ruby on Rails
How to Display Image Pointed by Url in Rails
Filtering Sensitive Data with Vcr
How to Convert JSON to Xml in Ruby
Devise with Ruby on Rails - Force User to Change Password on First Login
How to Create Line Breaks in Ruby
Automatically Logging Exceptions in Ruby
How to Make Two Thor Tasks Share Options
Filtering a List Using Checkboxes
Ruby: How to Convert an Array of Data to Hash and to JSON Format
How to Test Strong Params with Rspec
How to Set in a Middleware a Variable Accessible in All My Application
Sha1 Hashes Not Matching Between My Rails and Cocoa Apps
Ruby - Append Content at The End of The Existing S3 File Using Fog
How to Read Text from Non Visible Elements with Watir (Ruby)