# Formatting in ruby

**URL:** <https://www.ruby-forum.com/t/formatting-in-ruby/196699>\
**Category:** Ruby-Forum\
**Created:** [June 4, 2008, 9:41am UTC](https://www.ruby-forum.com/t/formatting-in-ruby/196699 "2008-06-04T09:41:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [June 4, 2008, 9:41am UTC](https://www.ruby-forum.com/t/formatting-in-ruby/196699/1 "2008-06-04T09:41:41Z")

</div>

Hi,  
I am changed simple format method and added first line code for  
replacing spaces  
for formatting in this it works but other code means new line  
paragraphs are not working?  
I want a such method which format all white spaces, new line,  
paragraphs.  
how it possible in ruby?

example:

def my\_format(text)  
text.gsub!(/(’ ')/, "&nbsp;")  
text.gsub!(/(\r\n|\n|\r)/, “\n”) # lets make them newlines  
crossplatform  
text.gsub!(/\n\n+/, “\n\n”) # zap dupes  
text.gsub!(/\n\n/, ‘

\0

’) # turn two newlines into paragraph  
text.gsub!(/([^\n])(\n)([^\n])/, ‘\1\2  
\3’) # turn single  
newline into  
content\_tag(“p”, text)  
end

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 25, 2008, 8:13am UTC](https://www.ruby-forum.com/t/formatting-in-ruby/196699/2 "2008-08-25T08:13:27Z")

</div>

You haven’t what your target format is. What do you want to replace all  
the white space with?

Sunny B. wrote:

> Hi,  
> I am changed simple format method and added first line code for  
> replacing spaces  
> for formatting in this it works but other code means new line  
> paragraphs are not working?  
> I want a such method which format all white spaces, new line,  
> paragraphs.
