何时使用块 [英] When to use blocks
问题描述
我喜欢 Ruby 块!它们背后的想法非常简洁和方便.
我刚刚回顾了过去一周左右的代码,基本上是我写过的每一个 ruby 函数,我注意到其中没有一个返回价值!我总是使用块来回传数据,而不是返回值!
我什至发现自己正在考虑编写一个小状态类,它可以让我编写如下代码:
something.do_stuff do |status|status.success 做# 成功代码结尾status.fail 做# 失败代码放置 status.error_message结尾结尾
我使用积木太多了吗?是否有时间使用块和时间使用返回值?
有什么需要注意的问题吗?我对积木的大量使用会在某个时候来咬我吗?
整个事情将更具可读性:
<前>if something.do_stuff#成功代码别的#失败的代码结尾
或使用共轨习语:
<前><代码>如果@user.save渲染:动作=>:显示别的@user.errors.each{|attr,msg|logger.info "#{attr} - #{msg}" }渲染:动作=>:编辑结尾恕我直言,避免返回布尔值是过度使用代码块.
如果 ...
它允许代码使用资源而不必关闭该资源
<前> open("fname") do |f|# 对文件做一些事情end #不必担心关闭文件
调用代码必须对结果进行非平凡的计算
在这种情况下,您避免将返回值添加到调用范围.对于多个返回值,这通常也很有意义.
<前>something.do_stuff 做 |res1, res2|如果 res1.foo?和 res2.bar?foo(res1)elsif res2.bar?酒吧(res2)结尾end #没有将 res1/res2 添加到调用范围
必须在yield之前和之后调用代码
您在一些 rails helper 中看到了这一点:
<前> <% content_tag :div do %><%= content_tag :span "跨度内容" %><% 结束 -%>
当然迭代器是一个很好的用例,因为它们(被ruby-ists认为)比for
循环或列表推导.
当然不是一个详尽的列表,但我建议您不要仅仅因为可以使用块.
I love Ruby blocks! The idea behind them is just very very neat and convenient.
I have just looked back over my code from the past week or so, which is basically every single ruby function I ever have written, and I have noticed that not a single one of them returns a value! Instead of returning values, I always use a block to pass the data back!
I have even caught myself contemplating writing a little status class which would allow me to write code like :
something.do_stuff do |status|
status.success do
# successful code
end
status.fail do
# fail code
puts status.error_message
end
end
Am I using blocks too much? Is there a time to use blocks and a time to use return values?
Are there any gotchas to be aware of? Will my huge use of blocks come and bite me sometime?
The whole thing would be more readable as:
if something.do_stuff
#successful code
else
#unsuccessful code
end
or to use a common rails idiom:
if @user.save
render :action=>:show
else
@user.errors.each{|attr,msg| logger.info "#{attr} - #{msg}" }
render :action=>:edit
end
IMHO, avoiding the return of a boolean value is overuse of code blocks.
A block makes sense if . . .
It allows code to use a resource without having to close that resource
open("fname") do |f|
# do stuff with the file
end #don't have to worry about closing the file
The calling code would have to do non-trivial computation with the result
In this case, you avoid adding the return value to calling scope. This also often makes sense with multiple return values.
something.do_stuff do |res1, res2|
if res1.foo? and res2.bar?
foo(res1)
elsif res2.bar?
bar(res2)
end
end #didn't add res1/res2 to the calling scope
Code must be called both before and after the yield
You see this in some of the rails helpers:
<% content_tag :div do %>
<%= content_tag :span "span content" %>
<% end -%>
And of course iterators are a great use case, as they're (considered by ruby-ists to be) prettier than for
loops or list comprehensions.
Certainly not an exhaustive list, but I recommend that you don't just use blocks because you can.
这篇关于何时使用块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!