我喜欢Ruby块!他们背后的想法非常简洁.
我刚刚回顾了过去一周左右的代码,这基本上是我写过的每一个ruby函数,我注意到它们中没有一个返回值!我总是使用一个块来传回数据,而不是返回值!
我甚至发现自己正在考虑编写一个小的状态类,这将允许我编写如下代码:
something.do_stuff do |status|
status.success do
# successful code
end
status.fail do
# fail code
puts status.error_message
end
end
Run Code Online (Sandbox Code Playgroud)
我是否过多使用积木?是否有时间使用块和时间来使用返回值?
有什么问题需要注意吗?我有多大时间使用积木来咬我吗?
klo*_*ner 16
整个事情将更具可读性:
if something.do_stuff
#successful code
else
#unsuccessful code
end
Run Code Online (Sandbox Code Playgroud)
或者使用共同的轨道成语:
if @user.save
render :action=>:show
else
@user.errors.each{|attr,msg| logger.info "#{attr} - #{msg}" }
render :action=>:edit
end
Run Code Online (Sandbox Code Playgroud)
恕我直言,避免返回布尔值是过度使用代码块.
如果,块是有道理的...
open("fname") do |f|
# do stuff with the file
end #don't have to worry about closing the file
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您可以避免将返回值添加到调用范围.对于多个返回值,这通常也是有意义的.
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
Run Code Online (Sandbox Code Playgroud)
你在一些铁路助手中看到了这一点:
<% content_tag :div do %>
<%= content_tag :span "span content" %>
<% end -%>
当然,迭代器是一个很好的用例,因为它们(被ruby-ists认为)比for循环或列表推导更漂亮.
当然不是一个详尽的列表,但我建议你不要只使用块,因为你可以.
我喜欢这种风格.它实际上非常类似于Ruby,通常你会看到项目重组他们的代码以使用这种格式而不是可读性较低的东西.
返回值在返回值有意义的地方是有意义的.如果您有Article对象,则需要article.title返回标题.但对于这个回调的特殊例子,它是一流的风格,你知道如何使用它们是好的.我怀疑Ruby的许多新手永远不知道如何做得好.