Ruby-on-rails Array.join("\n") 不是用换行符加入的方式吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1448864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:46:27  来源:igfitidea点击:

Array.join("\n") not the way to join with a newline?

ruby-on-railsruby

提问by jdimona

I have an array..

我有一个数组..

[1,2,3,4]

and I want a string containing all the elements separated by a newline..

我想要一个包含由换行符分隔的所有元素的字符串..

1

2

3

4

but when I try [1,2,3,4].join("\n")I get

但是当我尝试[1,2,3,4].join("\n")我得到

1\n2\n3\n4

I feel like there is an obvious answer but I can't find it!

我觉得有一个明显的答案,但我找不到!

回答by Cody Caughlan

Yes, but if you print that string out it will have newlines in it:

是的,但是如果您打印出该字符串,它将在其中包含换行符:

irb(main):001:0> a = (1..4).to_a
=> [1, 2, 3, 4]
irb(main):002:0> a.join("\n")
=> "1\n2\n3\n4"
irb(main):003:0> puts a.join("\n")
1
2
3
4

So it does appear to achieve what you desire (?)

所以它似乎实现了你想要的(?)

回答by Screamer

A subtle error that can occur here is to use single quotes instead of double. That also has the effect of rendering the newlines as \n. So

这里可能发生的一个微妙错误是使用单引号而不是双引号。这也具有将换行符呈现为 \n 的效果。所以

puts a.join("\n")   # correct

is not the same as

不一样

puts a.join('\n')   # incorrect

There is an excellent write up on why this is the case here.

还有为什么是这样一个优秀的写了这里

回答by Ajay

Just in case anyone searching for this functionality in ERB template then try this :

以防万一有人在 ERB 模板中搜索此功能,请尝试以下操作:

(1..5).to_a.join("<br>").html_safe

回答by khelll

Try this also:

也试试这个:

puts (1..4).to_a * "\n"

回答by Allison

You may not want to use html_safe, like ajay said, depending on context. Html safe can be a security issue. This depends on if the original input was actuallyhtml safe. HTML safe should not be called on input direct from a user and typically should be called before the view.

根据上下文,您可能不想使用 html_safe,就像 ajay 所说的那样。Html 安全可能是一个安全问题。这取决于原始输入是否实际上是html 安全的。HTML 安全不应在用户直接输入时调用,通常应在视图之前调用。

https://bibwild.wordpress.com/2013/12/19/you-never-want-to-call-html_safe-in-a-rails-template/

https://bibwild.wordpress.com/2013/12/19/you-never-want-to-call-html_safe-in-a-rails-template/

[edited in response to comment below]

[编辑以回应下面的评论]

回答by staxim

As some of the other answers above imply, Rails may be escaping your code before rendering as html. Here's a sample that addresses this problem (first sanitizing the inputs, so that you can "safely" call html_safeon the result):

正如上面的一些其他答案所暗示的那样,Rails 可能会在呈现为 html 之前转义您的代码。这是解决此问题的示例(首先清理输入,以便您可以“安全地”调用html_safe结果):

my_array = [1, 2, 3, 4]
my_array.map{ |i| i.to_s.sanitize }.join("\n").html_safe

You only need sanitizeif you don't trust the inputs.

sanitize当您不信任输入时才需要。

回答by Hetal Khunti

How about this If you wanted to print each element on new line..

这个怎么样如果你想在新行上打印每个元素..

> a = [1, 2, 3, 4] 
> a.each{|e| puts e}
1
2
3
4
 => [1, 2, 3, 4] 

回答by Eddie

Use the Enter or Return key on your typing surface. .-.

在打字面上使用 Enter 或 Return 键。.-.

[1,2,3,4].join("

[1,2,3,4].join("

")

”)