从 Ruby on Rails 块输出代码,无需关闭 ruby 标签
好吧,这可能看起来很愚蠢,我已经使用各种不同的关键字和术语进行了广泛的谷歌搜索,尝试了很多不同的方法,并尝试了我最后的广度,但似乎无法弄清楚如何做到这一点。我想要的只是 Rails 应用程序中的标准输出,而不需要打破我的障碍。
目前,当我在 Rails 应用程序中执行一段 ruby 代码并想要输出值时,我执行以下操作
<% somearray do | item | %>
<%= item %>
<% end %>
对我来说,这非常混乱,我知道来自 PHP 背景的人不应该想象如何在 Rails 中编码,而是来自许多不同环境,我希望我的代码对我来说看起来不错。用 <% %> 封装每一行是非常容易的。但我更希望我的代码是我的代码,我的标记是我的标记。
我想要的,但不起作用的是......
<%
somearray do | item |
whatever item #whatever being puts or similar
end
%>
这只是为了代码的美观而不是为了它产生的结果,但对于我来说,一个很长时间的程序员,这意味着很多很多尝试查看代码块开始和结束的位置和时间时可以节省时间。
提前致谢,
杰 (c0ld)
Ok this may seem stupid and I have googled extensively using various different keywords and terms, attempted a lot of various different methods and tried to my very last breadth and cannot seem to figure out how to do this. All I want is stdout in a rails app without needing to break my block.
Currently, when I do a block of ruby code in my rails app and want to output the value I do the following
<% somearray do | item | %>
<%= item %>
<% end %>
To me this is quite messy, I know coming from a PHP background it's not how one should imagine how to code in rails but coming from many different environments I like my code to look nice to me. It's very easy to encapsulate every single line with <% %> but I'd prefer my code to be my code and my mark up to be my mark up.
What I'd like, but which doesn't work, is....
<%
somearray do | item |
whatever item #whatever being puts or similar
end
%>
It's solely for the aesthetics of the code and not for what it produces but for me, a very long time programmer, it means a lot and a lot of time saved when trying to see where and when code blocks begin and end.
Thanks in advance,
Jay (c0ld)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
concat
。来自文档:You can use
concat
. From the docs:怎么样...
PS当然如果item只是输出自己 - 它可以更短
What about...
P.S. of course if item just outputs itself - it can be even shorter