simple_format 更改文本本身

simple_format changes the text itself

在 Rails 3.0 中,辅助方法 simple_format 更改参数本身。 我预计它只是 returns 换行的文本。

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>"

我检查了 Rails 4.2,它没有更改文本。

有人可以解释一下吗?

山姆

此方法在 Rails 4.2Rails 3.0 中的不同之处在于,在 Rails 3.0 中,传递的字符串被修改(由 gsub! 改变),而在 Rails 4.2 不是(它只是 returns 一个新修改的字符串):

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"

可以在documentation

中找到不同实现的源代码