我们最近在工作中决定采用红宝石风格指南.其中一条规则是任何行都不应超过80个字符.由于这是一个Rails项目,我们经常会有一些更长的字符串 - 即" 用户X想要向您发送有关Thing Y的消息 ",这并不总是符合80个字符的样式限制.
我知道有三种方法可以让长字符串跨越多行:
然而,所有这些情况最终都需要更多的计算周期,这看起来很愚蠢.字符串连接显然,但是HEREDOC
,%Q
我必须通过类似的方式删除换行符.gsub(/\n$/, '')
.
有没有纯语法方法来做到这一点,这相当于只需要将整个字符串放在一行上?显然,目标是不要花费任何额外的周期,因为我希望我的代码更具可读性.(是的,我意识到你必须做出很多权衡......但是对于字符串长度来说,这看起来很愚蠢.)
更新:反斜杠不是我想要的,因为你会丢失缩进,这实际上会影响样式/可读性.
例:
if foo
string = "this is a \
string that spans lines"
end
Run Code Online (Sandbox Code Playgroud)
我发现上面有点难以阅读.
编辑:我在下面添加了一个答案; 三年后,我们现在有了波浪形的heredoc.
我想使用Ajax在模态窗口中插入一些东西,所以我尝试手动打开模态窗口.代码看起来像这样
$(function(){
$('#modal-from-dom').modal({
backdrop: true,
keyboard: true
});
$('#modalbutton').click(function(){
$('#my-modal').bind('show', function () {
// do sth with the modal
});
$('#modal-from-dom').modal('toggle');
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
HTML直接从bootstrap js页面复制
<div id="modal-from-dom" class="modal hide fade">
<div class="modal-header">
<a href="#" class="close">×</a>
<h3>Modal Heading</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<a href="#" class="btn primary">Primary</a>
<a href="#" class="btn secondary">Secondary</a>
</div>
</div>
<button id="modalbutton" class="btn">Launch Modal</button>
Run Code Online (Sandbox Code Playgroud)
所以问题是第一次点击按钮似乎工作正常,在第二次点击模式窗口显示1秒左右然后消失.如果我将"切换"更改为"显示",则在第二次单击后,背景将不会完全淡出.我该怎么调试呢?
是否有一个Rails/Ruby习惯用于检查枚举是否同时存在且具有非nil
值?
如果我尝试这样做nil.any?
,我会遇到错误,我总是这样做if foo && foo.any?
.