Ruby:意外的输入结束,if 语句需要关键字结束 [英] Ruby: unexpected end-of-input, expecting keyword_end for if statement

查看:46
本文介绍了Ruby:意外的输入结束,if 语句需要关键字结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Java 工作了几个月,现在正在过渡回 Ruby.我从以下代码中收到一个奇怪的错误:

I was working with Java for a few months and am transitioning back to Ruby now. I am getting a weird error from the following code:

def count_divisors
  divisor_hash = {}
  25.times do |i|
    divisor_hash[i] = find_dividends(i)
  end
  puts divisor_hash
end

def find_dividends(i)
  count = 0
  1000.times do |k|
    if i % ( k + 1 ) == 0
      count++
    end
  end
  count
end
count_divisors()

此代码生成以下错误:

test.rb:14: syntax error, unexpected keyword_end
test.rb:19: syntax error, unexpected end-of-input, expecting keyword_end

当我删除 if 语句时不会发生此错误.我不知道为什么.我知道每个 if 语句都需要一个 end 语句,但出于某种原因,该 end 语句的位置似乎令人不安.

This error does not occur when I remove the if statement. I am not sure why. I know every if statement needs an end statement, for some reason it seems upset by the placement of that end statement though.

推荐答案

count++ 更改为 count += 1 Ruby 不支持增量运算符.

Change the count++ to count += 1 Ruby does not support incremental operator.

这篇关于Ruby:意外的输入结束,if 语句需要关键字结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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