Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否可以检查(以及如何)检查文本的行数?如果我对某些文本使用 simple_format,它会换行,但我需要的是检查是否有不超过 3 行的文本。
代码:
<%= simple_format haiku.content %>
simple_format遇到\n 时会插入<br />,遇到\n\n 时会插入<p></p> 标签。考虑到这一点,以下解决方案允许灵活地构建输入字符串。
# split on 1 or more new line (\n) lines = simple_format(haiku.content).split(/\n+/) # check array size lines.size <= 3