I want to split a string by whitespace
irb(main):001:0> input = "dog cat"
=> "dog cat"
irb(main):002:0> output = input.strip.split(/\s+/)
=> ["dog", "cat"]
This is good. However, I'm also doing this in the controller in Rails, and when I supply the same input, and have it print out the output #{output} into my view, it shows as dogcat instead of ["dog", "cat"]. I am really confused how that can happen. Any ideas?
I'm printing it using #notice = "#{output}" in the controller, and in my view I have <%= #notice %>
Rather than splitting your string in the controller and sending it as an array to your view, send the entire string to your view:
input = "dog cat"
#notice = input
Then, in your view, split your the string and display it as a stringified-array:
<%= array(#notice.strip.split(/\s+/)).to_s %>
If you print an array of strings, you'll get the strings all concatenated together. You'd get the same thing in irb if you had entered, print "#{output}". You need to decide how you want to format them and print them that way, perhaps with a simple helper function. For example, the helper could do:
output.each { |s| puts "<p>#{s}</p>" }
Or whatever you like.
Continuing with your example code:
>> input = "dog cat"
=> "dog cat"
>> output = input.strip.split /\s+/
=> ["dog", "cat"]
>> joined = output.join ' '
=> "dog cat"
Remember too that Ruby has several helpers like %w and %W for letting you convert a string into an array of words. If you're starting with an array of words, each of which may have whitespace before and after its individual item, you might try something like this:
>> # `input` is an array of words that was populated Somewhere Else
>> # `input` has the initial value [" dog ", "cat\n", "\r tribble\t"]
>> output = input.join.split /\s+/
=> ["dog", "cat", "tribble"]
>> joined = output.join ' '
=> "dog cat tribble"
Calling String#join without any parameter will join stringish array items together with no separation between them, and is what seems to be done in your example where you just render the array as a string
>> #notice = output
>> # #notice will render as 'dogcat'
As opposed to:
>> #notice = input.join.split(/\s+/).join ' '
>> # #notice will render as 'dog cat'
And there you go.
Related
I am trying to replace Space in a string with Line Break in Ruby on Rails,
name = 'john smith'
i have tried the following so far:
name.gsub!(" ", "\n")
name.gsub!(" ", "<br>")
name.sub(" ", "\n")
name.sub(" ", "<br>")
but none of the above worked.
You have to be careful when marking a string as html_safe, especially if it may contain user input:
name = 'john smith<script>alert("gotcha")</script>'
name.gsub(' ', '<br>').html_safe
#=> "john<br>smith<script>alert(\"gotcha\")</script>"
Rails would output that string as-is, i.e. including the <script> tag.
In order to take advantage of Rails' HTML escaping, you should only mark the trusted parts as being html_safe. For a manually concatenated string:
''.html_safe + 'john' + '<br>'.html_safe + 'smith<script>alert("gotcha")</script>'
#=> "john<br>smith<script>alert("gotcha")</script>"
As you can see, only the <br> tag was left intact, the remaining parts were properly escaped.
There are several helpers for building safe strings as well as for building HTML tags. In your case, I'd use safe_join and tag:
name = 'john smith<script>alert("gotcha")</script>'
safe_join(name.split(' '), tag(:br))
#=> "john<br />smith<script>alert("gotcha")</script>"
While printing it in html you will need to use raw, otherwise rails will escape the tags
= raw name.gsub(" ", "<br>")
Try another one:
<%= name.gsub(" ", "<br>").html_safe %>
html_safe :
Marks a string as trusted safe. It will be inserted into HTML with no additional escaping performed.
"<a>Hello</a>".html_safe
#=> "<a>Hello</a>"
nil.html_safe
#=> NoMethodError: undefined method `html_safe' for nil:NilClass
raw :
raw is just a wrapper around html_safe. Use raw if there are chances that the string will be nil.
raw("<a>Hello</a>")
#=> "<a>Hello</a>"
raw(nil)
#=> ""
I am trying to get rid of all the extra <br> in the first paragraph and last paragraph.
For example:
st = "<p><br><br><br><br>apple</p>
<p>bananas</p>
<p>orange<br><br><br><br><br></p>
<p>tomatoes</p>
<p>berry<br><br><br><br><br><br></p>"
I'm hoping to end up with this:
"<p>apple</p>
<p>bananas</p>
<p>orange<br><br><br><br><br></p>
<p>tomatoes</p>
<p>berry</p>"
My goal is to leave the <br> middle paragraphs (ex. orange paragraph) alone and remove all the first paragraph <br> and all the end the last paragraph.
I've tried doing this regex:
st.sub(/^((<p>)|<br( \/)?>)*|(<p>|<br( \/)?>|< \/p>)*$/, '')
I get this:
=> "<p>apple</p>
<p>bananas</p>
<p>orange<br><br><br><br><br></p>
<p>tomatoes</p>
<p>berry<br><br><br><br><br><br></p>"
I am unable to delete the last paragraph repeating <br>.
Don't use regular expressions. Instead use a parser:
require 'nokogiri'
doc = Nokogiri::HTML::DocumentFragment.parse(<<EOT)
<p><br><br><br><br>apple</p>
<p>bananas</p>
<p>orange<br><br><br><br><br></p>
<p>tomatoes</p>
<p>berry<br><br><br><br><br><br></p>
EOT
p_tags = doc.search('p')
[:first, :last].each { |s| p_tags.send(s).search('br').remove }
doc.to_html
Which would result in the fragment looking like:
# => "<p>apple</p>\n" +
# "<p>bananas</p>\n" +
# "<p>orange<br><br><br><br><br></p>\n" +
# "<p>tomatoes</p>\n" +
# "<p>berry</p>\n"
Parsers are much more able to cope with changing HTML so if you're going to do any HTML changes or scraping it pays off to learn how to use them.
An alternate way to do what you want without a parser or a complicated regex is:
str = <<EOT
<p><br><br><br><br>apple</p>
<p>bananas</p>
<p>orange<br><br><br><br><br></p>
<p>tomatoes</p>
<p>berry<br><br><br><br><br><br></p>
EOT
str_lines = str.lines
[0, -1].each { |i| str_lines[i].gsub!(/<br>/, '') }
puts str_lines.join
Which results in the same thing.
The strength of the first method is that it won't care if the <br> mysteriously change to <br/> as in HTML5, or <br >.
Finally, if you doubly insist on using a longer, more complicated, pattern, at least simplify it:
puts str.sub(/\A<p>(?:<br>)+/, '<p>').sub(/(?:<br>)+<\/p>\Z/, '</p>')
which results in the same thing again.
Regular expressions are great for some tasks, but they're not good for markup. If you insist on using a regular expression, then simplify the problem as in the later solutions because it reduces the complexity of the pattern, which improves readability and eases maintenance.
st = st.gsub(/(?<=\A<p>)(<br\/?>)+|(<br\/?>)+(?=[<]\/p>\Z)/, '')
There's 2 parts seperated by a pipe (OR):
1) (?<=\A<p>)(<br\/?>)+ matches 1 or more <br> that are after the start of the string (\A) and a <p> tag
2) (<br\/?>)+(?=[<]\/p>\Z) matches matches 1 or more <br> that are before a </p> closing tag at the end of the string (\Z)
And gsub because we want to replace all occurrences in the string, not just the first.
The g in gsub stands for global.
I suggest something simple that's easy to understand, test and maintain.
str =<<-_
<p><br><br><br><br>apple</p>
<p>bananas</p>
<p>orange<br><br><br><br><br></p>
<p>tomatoes</p>
<p>berry<br><br><br><br><br><br></p>
_
#=> "<p><br><br><br><br>apple</p>\n<p>bananas</p>\n<p>orange<br><br><br><br><br></p>\n<p>tomatoes</p>\n<p>berry<br><br><br><br><br><br></p>\n"
first, *mid, last = str.lines
first.gsub('<br>', '') << mid.join << last.gsub('<br>', '')
#=> "<p>apple</p>\n<p>bananas</p>\n<p>orange<br><br><br><br><br></p>\n<p>tomatoes</p>\n<p>berry</p>\n"
puts s
<p>apple</p>
<p>bananas</p>
<p>orange<br><br><br><br><br></p>
<p>tomatoes</p>
<p>berry</p>
Note that
first
#=> "<p><br><br><br><br>apple</p>\n"
mid
#=> ["<p>bananas</p>\n",
# "<p>orange<br><br><br><br><br></p>\n",
# "<p>tomatoes</p>\n"]
last
#=> "<p>berry<br><br><br><br><br><br></p>\n"
I have a page where I take input from a form and then display it in a view.
View:
<%= form_tag("/page", method:"get") do %>
<%= text_area_tag(:x, #input)%>
<%= submit_tag("Submit Form") %> <%end%>
<%=#input%>
Controller:
def myMethod
if params[:x].present?
#input = "#{params[:x]}"
end
This works fine however I want to be able to identify where there are spaces in the string and then replace the spaces with a new line, and add a ",". For example, if the user inputs ‘cat dog mouse’ i want the view to return:
'cat',
'dog',
'mouse',
Is there an easy way to do this with a ruby function or will I need to write a regular expressions text search?
Thanks
A simple gsub will do:
"cat dog mouse".gsub(" ", ",\n")
This will replace every occurrence of a space with a comma/newline ,.
Update
Since you want to encapsulate each line with single quotes, a simple way to do it would be:
"cat dog mouse".split # Split the string into an array (automatically splits by space)
.map{|w| "'#{w}'"}} # Reassemble it with single quotes added
.join(",\n") # Convert the array into a string again and insert the comma/newline characters between each entry
That code, of course, can all be written on one line.
Here's another quick way to do this:
string = "cat dog mouse"
new_string = "'" + string.split.join("',\n'") + "'" # Outputs the same as above. Less friendly to read, but is also shorter.
You can use split, map and join:
"cat dog mouse".split(" ").map {|a| "'#{a}',\n"}.join
split creates a list ["cat", "dog", "mouse"]
map transforms it ["'cat',\n", "'dog',\n", "'mouse',\n"]
join creates a string again "'cat',\n'dog',\n'mouse',\n"
I'd like to debug a variable #num in my Rails controller, so I'm evaluating
<%= #num %>
in my Rails view. However, I cannot distinguish between #num being '', ' ', and ' ' (and other different types of whitespace) when it's printed in the HTML. Is there any way to print it out clearly?
If you want to be really sure:
<%= #num.inspect %>
When #num = ' ' this should render:
" "
#inspect is great when you want to a representation of some object as a string.
If this is a complex object or large array or deep hash, I often prefer #to_yaml for inspection which lays it out in a somewhat readable format.
# controller
#foo = {:a => [:bar, :baz], :b => 123, :c => 'omg'}
# view
<pre><%= #foo.to_yaml %></pre>
# visible output
---
:b: 123
:c: omg
:a:
- :bar
- :baz
<%=debug #num %>
Will format it in haml.
<%= #num.inspect %>
Will format it as "p" does.
See Debugging Rails Applications (rails guides)
<%= message.content %>
I can display a message like this, but in some situations I would like to display only the first 5 words of the string and then show an ellipsis (...)
In rails 4.2 you can use truncate_words.
'Once upon a time in a world far far away'.truncate_words(4)
=> "Once upon a time..."
you can use truncate to limit length of string
truncate("Once upon a time in a world far far away", :length => 17, :separator => ' ')
# => "Once upon a..."
with given space separator it won't cut your words.
If you want exactly 5 words you can do something like this
class String
def words_limit(limit)
string_arr = self.split(' ')
string_arr.count > limit ? "#{string_arr[0..(limit-1)].join(' ')}..." : self
end
end
text = "aa bb cc dd ee ff"
p text.words_limit(3)
# => aa bb cc...
Try the following:
'this is a line of some words'.split[0..3].join(' ')
=> "this is a line"
# Message helper
def content_excerpt(c)
return unlessc
c.split(" ")[0..4].join + "..."
end
# View
<%= message.content_excerpt %>
But the common way is truncate method
# Message helper
def content_excerpt(c)
return unless c
truncate(c, :length => 20)
end