Ruby on Rails:将每个循环嵌套在while循环内会产生错误 [英] Ruby on Rails: nested each loop within while loop generates error

查看:95
本文介绍了Ruby on Rails:将每个循环嵌套在while循环内会产生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在rails控制台中执行以下代码时,出现语法错误: 语法错误,意外的$ end,需要keyword_end"

I get a syntax error, when I execute the following code within the rails console: "syntax error, unexpected $end, expecting keyword_end"

range = 2
my_array = Array.new(range)
a = [1]
i = 0
while i < range do
  a.each do |b|
    puts "test"
  end
  i += 1
end

有人知道我在做什么错吗?奇怪的是,代码在我的服务器上的ruby文件中正常工作.

Does anyone know what I am doing wrong? The strange thing is, that the code is working on my server within a ruby file.

非常感谢!

tuxware

推荐答案

我也遇到过类似的问题

I have faced similar issue posted here. if we drop (optional) do in the line with a while loop, then this works even in IRB.

range = 2
my_array = Array.new(range)
a = [1]
i = 0
while i < range
  a.each do |b|
    puts "test"
  end
  i += 1
end

这篇关于Ruby on Rails:将每个循环嵌套在while循环内会产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆