简体   繁体   English

卡在Ruby上的Code Academy

[英]Stuck with Ruby on Code Academy

I am doing Code Academy The Refactor Factory: Less is More (Exercise 4) module. 我正在执行代码学院“重构工厂:少即是多(练习4)”模块。 Here are the instructions for the module: 以下是该模块的说明:

Less is More Great work! 少即是多伟大的工作! This code looks better already. 这段代码看起来已经更好了。

We can remove even more, however. 但是,我们可以删除更多。 Recall that Ruby will implicitly (that is, automatically) return the value of the last expression it evaluates. 回想一下,Ruby将隐式(即自动)返回它评估的最后一个表达式的值。 There's one return statement in this code that we can change from explicit to implicit! 这段代码中有一个return语句,我们可以从显式更改为隐式!

Instructions Find the unnecessary return statement and remove it. 说明查找并删除不必要的return语句。

Hint: it's the last one! 提示:这是最后一个! This is because we may want to return early if the input isn't a positive number, and we can't return early unless we explicitly use the return keyword. 这是因为如果输入不是正数,我们可能希望提早返回,并且除非明确使用return关键字,否则我们不会提早返回。

I tried to remove the last return statement and each of the others, and I get an error. 我试图删除最后一个return语句以及其他每个语句,但出现错误。 Can anyone offer any suggestions? 谁能提供任何建议?

$VERBOSE = nil    # We'll explain this at the end of the lesson.
require 'prime'   # This is a module. We'll cover these soon!

def first_n_primes(n)

  return "n must be an integer." unless n.is_a? Integer

  return "n must be greater than 0." if n <= 0

  prime_array ||= []

  prime = Prime.new
  for num in (1..n)
    prime_array.push(prime.next)
  end
  return prime_array
end

first_n_primes(10)

取出returnreturn prime_array应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM