如何测试块是否为空?
我有一个代码块,我想在不运行代码块内部的代码的情况下测试主体是否为空。这可能吗?
I have a block of code and I'd like to test if the body is empty without running the code inside of the block. Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sourcify
gem 添加了一个Proc#to_source
方法:将块作为字符串后,就很容易看出大括号之间是否有注释(或只有空格)。
The
sourcify
gem adds aProc#to_source
method:Once you have the block as a string it's fairly easy to see if there's noting (or only whitespace) between the curly braces.
更新:Ruby 2.0+ 删除了块比较,因此不再可能仅使用内置方法。
Ruby 过去常常比较
Proc
,但不太擅长。例如,您可以:因此,决定删除块比较,因此两个块现在
==
,前提是它们是同一对象。Update: Ruby 2.0+ removed block comparison, so it is no longer possible using only the builtin methods.
Ruby used to compare
Proc
s, but was not great at it. For instance you could:Because of this, it was decided to remove the block comparison, so two blocks are now
==
iff they are the same object.