大佬教程收集整理的这篇文章主要介绍了Ruby在while循环期间会忘记局部变量?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我已经建立了一个简单的程序来做到这一点,但是我看到一些令我震惊的东西:看起来Ruby正在忘记局部变量存在 – 或者我发现编程错误? [虽然我不认为我有:如果我在我的循环之前定义变量’message’,我看不到错误].
flag=false # message=nil # this is will prevent the issue. while line=gets do if line =~/Hello/ then if flag==true then puts "#{messagE}" end message=StringIO.new(linE); puts message flag=true else message << line end end # Input File example: # Hello this is a record # this is also part of the same record # Hello this is a new record # this is still record 2 # Hello this is record 3 etc etc # # Error when running: [nb,first iteration is fine] # <StringIO:0x2e845ac> # Hello # test.rb:5: undefined local variable or method `message' for main:Object (NameError) #
您可以在每次循环迭代开始时输出消息的值,以测试我的建议是否正确.
以上是大佬教程为你收集整理的Ruby在while循环期间会忘记局部变量?全部内容,希望文章能够帮你解决Ruby在while循环期间会忘记局部变量?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。