Sam Kong
Sam Kong

Reputation: 5810

simple_format changes the text itself

In Rails 3.0, the helper method simple_format changes the parameter itself. I expected that it only returns the wrapped text.

2.0.0-p648 :001 > Rails.version
 => "3.0.20"
2.0.0-p648 :002 > s = "Hello"
 => "Hello"
2.0.0-p648 :003 > helper.simple_format(s)
 => "<p>Hello</p>"
2.0.0-p648 :004 > s
 => "<p>Hello</p>"

I checked with Rails 4.2 and it doesn't change the text.

Can someone please explain it?

Sam

Upvotes: 2

Views: 63

Answers (1)

Igor Drozdov
Igor Drozdov

Reputation: 15045

The difference between implementations of this method in Rails 4.2 and Rails 3.0 is that in Rails 3.0 the passed string is modified (mutated by gsub!) and in Rails 4.2 it's not (it just returns a new modified string):

Rails 4.2:

2.4.0 :006 > s = "hello"
 => "hello"
2.4.0 :007 > simple_format s
 => "<p>hello</p>"
2.4.0 :008 > s
 => "hello"

The source code of different implementations can be found in the documentation

Upvotes: 5

Related Questions