ruby有一些边缘情况很难解释,因为解析会带来一些有趣的问题.我在这里列出其中两个.如果你知道更多,那么添加到列表中.
def foo 5 end # this one works if (tmp = foo) puts tmp.to_s end # However if you attempt to squeeze the above # three lines into one line then code will fail # take a look at this one. I am naming tmp2 to # avoid any side effect # Error: undefined local variable or method ‘tmp2’ for main:Object puts tmp2.to_s if (tmp2 = foo)
这是另一个.
def x 4 end def y x = 1 if false x + 2 end # Error: undefined method `+' for nil:NilClass puts y
但是,如果您注释掉x = 1行,如果为false,则代码将正常工作.
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。