# Ruby's simple\_format method is not working for spaces

**URL:** <https://www.ruby-forum.com/t/rubys-simple-format-method-is-not-working-for-spaces/138945>\
**Category:** Rails\
**Created:** [May 29, 2008, 8:17am UTC](https://www.ruby-forum.com/t/rubys-simple-format-method-is-not-working-for-spaces/138945 "2008-05-29T08:17:21Z")\
**Posts on this page:** 5\
**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:** [May 29, 2008, 8:17am UTC](https://www.ruby-forum.com/t/rubys-simple-format-method-is-not-working-for-spaces/138945/1 "2008-05-29T08:17:21Z")

</div>

Ruby’s simple\_format method is not working for spaces. so how to add and  
what to add in simple format method code so it also work for spaces?

---

<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:** [May 29, 2008, 11:02am UTC](https://www.ruby-forum.com/t/rubys-simple-format-method-is-not-working-for-spaces/138945/2 "2008-05-29T11:02:36Z")

</div>

On 29 May 2008, at 07:17, Sunny B. wrote:

> Ruby’s simple\_format method is not working for spaces. so how to add  
> and  
> what to add in simple format method code so it also work for spaces?

What are you expecting it to do to spaces? It’s only suppose to  
convert newlines to paragraph/  
 tags

Fred

---

<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:** [May 29, 2008, 11:19am UTC](https://www.ruby-forum.com/t/rubys-simple-format-method-is-not-working-for-spaces/138945/3 "2008-05-29T11:19:45Z")

</div>

Frederick C. wrote:

> On 29 May 2008, at 07:17, Sunny B. wrote:
> 
> > Ruby’s simple\_format method is not working for spaces. so how to add  
> > and  
> > what to add in simple format method code so it also work for spaces?
> 
> What are you expecting it to do to spaces? It’s only suppose to  
> convert newlines to paragraph/  
> tags
> 
> Fred

But if i want to show whatever i typed in text box same to UI (including  
spaces,newline, tab ) then there is another way doing it in ruby.

---

<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:** [May 29, 2008, 12:16pm UTC](https://www.ruby-forum.com/t/rubys-simple-format-method-is-not-working-for-spaces/138945/4 "2008-05-29T12:16:21Z")

</div>

Frederick C. wrote:

> On 29 May 2008, at 10:19, Sunny B. wrote:
> 
> > > convert newlines to paragraph/  
> > > tags
> > > 
> > > Fred
> > 
> > But if i want to show whatever i typed in text box same to UI  
> > (including  
> > spaces,newline, tab ) then there is another way doing it in ruby.
> 
> Replace spaces with &nbsp; or enclose everything in
> 
> ```
> Fred
> ```

I am trying this replaces all spaces with &nbsp; then other code is not  
working means(new line paragraph)  
I am writing following method  
def simple\_format\_include\_spaces(text)  
content\_tag ‘p’, text.to\_s.  
gsub(/[" "]/, "&nbsp;" ).  
gsub(/\r\n?/, “\n”). # \r\n and \r → \n  
gsub(/\n\n+/, “

\n\n

”). # 2+ newline → paragraph  
gsub(/([^\n]\n)(?=[^\n])/, ‘\1  
’) # 1 newline → br  
end

but it not working. and pre tag is also not work for spaces

---

<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:** [May 29, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/rubys-simple-format-method-is-not-working-for-spaces/138945/5 "2008-05-29T12:11:45Z")

</div>

On 29 May 2008, at 10:19, Sunny B. wrote:

> > convert newlines to paragraph/  
> > tags
> > 
> > Fred
> 
> But if i want to show whatever i typed in text box same to UI  
> (including  
> spaces,newline, tab ) then there is another way doing it in ruby.

Replace spaces with &nbsp; or enclose everything in

```
Fred
```
