Ruby Koans 评分项目。未定义的方法“长度”对于 nil:NilClass

发布于 2024-12-05 17:27:58 字数 912 浏览 2 评论 0原文

我对 Ruby 和一般编程都是全新的。我正在研究 Ruby Koans。在卡住之前我已经达到了 176/274。
这是“评分项目”,我需要编写一个方法来计算给定骰子的得分。
这可能不是您见过的最优雅的代码,但这是我想到的:

def score(dice)
  tally = 0
  tally += (dice.sort.to_s[/[1]+/].length % 3) * 100
  if dice.sort.to_s[/[1]+/].length >= 3
    tally += 1000
  end
  tally = tally + (dice.sort.to_s[/[5]+/].length % 3) * 50
  if dice.sort.to_s[/[5]+/].length >= 3
    tally += 500
  end
  if dice.sort.to_s[/[2]+/].length >= 3
    tally += 200
  end
  if dice.sort.to_s[/[3]+/].length >= 3
    tally += 300
  end
  if dice.sort.to_s[/[4]+/].length >= 3
    tally += 400
  end
  if dice.sort.to_s[/[6]+/].length >= 3
    tally += 600
  end
  return tally
end

第一个测试是:score([]) 需要返回 0

当我运行它时,我得到“undefined method `length' for nil: NilClass”(引用的行是 .length 的第一个实例) 这告诉我“dice.sort.to_s[/[1]+/]”和“score([])”为零,但是当我在 irb>> 中运行它时它是 0。

什么给出?

I am BRAND new to Ruby, and programing in general. I'm working my way through the Ruby Koans. I've made it up to 176/274 before getting stuck.
It's the "Scoring Project" I need to write a method to calculate the score of a given dice roll.
This may not be the most elegant code you've ever seen but here's what I came up with:

def score(dice)
  tally = 0
  tally += (dice.sort.to_s[/[1]+/].length % 3) * 100
  if dice.sort.to_s[/[1]+/].length >= 3
    tally += 1000
  end
  tally = tally + (dice.sort.to_s[/[5]+/].length % 3) * 50
  if dice.sort.to_s[/[5]+/].length >= 3
    tally += 500
  end
  if dice.sort.to_s[/[2]+/].length >= 3
    tally += 200
  end
  if dice.sort.to_s[/[3]+/].length >= 3
    tally += 300
  end
  if dice.sort.to_s[/[4]+/].length >= 3
    tally += 400
  end
  if dice.sort.to_s[/[6]+/].length >= 3
    tally += 600
  end
  return tally
end

The first test is: score([]) needs to return 0

When I run it I get "undefined method `length' for nil:NilClass" (the line referenced is the first instance of .length)
This tells me that "dice.sort.to_s[/[1]+/]" with "score([])" is nil, but when i run it in irb>> it is 0.

What gives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由范儿 2024-12-12 17:27:58

好的。知道了。

非常抱歉,我说过运行“dice.sort.to_s[/[1]+/]”返回零而不是零,这一定是因为它有一些我不知道的存储值。当我运行“[].sort.to_s[/[1]+/]”时,它正确返回零。因此,我将每个 if 语句嵌套在一个检查中,以确保不存在 nil 值。

def score(dice)
  tally = 0
  if dice.sort.to_s[/[1]+/]
    tally += (dice.sort.to_s[/[1]+/].length % 3) * 100
    if dice.sort.to_s[/[1]+/].length >= 3
      tally += 1000
    end
  end
  if dice.sort.to_s[/[5]+/]
    tally += (dice.sort.to_s[/[5]+/].length % 3) * 50
    if dice.sort.to_s[/[5]+/].length >= 3
      tally += 500
    end
  end
  if dice.sort.to_s[/[2]+/]
    if dice.sort.to_s[/[2]+/].length >= 3
      tally += 200
    end
  end
  if dice.sort.to_s[/[3]+/]
    if dice.sort.to_s[/[3]+/].length >= 3
      tally += 300
    end
  end
  if dice.sort.to_s[/[4]+/]
    if dice.sort.to_s[/[4]+/].length >= 3
      tally += 400
    end
  end
  if dice.sort.to_s[/[6]+/]
    if dice.sort.to_s[/[6]+/].length >= 3
      tally += 600
    end
  end
  return tally
end

所有测试都通过了。

OK. Got it.

Huge apologies, I had said that running "dice.sort.to_s[/[1]+/]" was returning zero not nil, it must have been because it had some storred value i didn't know about. When i ran "[].sort.to_s[/[1]+/]" it returned nil properly. So I nested each of my if statements in a check to make sure there wasn't a nil value.

def score(dice)
  tally = 0
  if dice.sort.to_s[/[1]+/]
    tally += (dice.sort.to_s[/[1]+/].length % 3) * 100
    if dice.sort.to_s[/[1]+/].length >= 3
      tally += 1000
    end
  end
  if dice.sort.to_s[/[5]+/]
    tally += (dice.sort.to_s[/[5]+/].length % 3) * 50
    if dice.sort.to_s[/[5]+/].length >= 3
      tally += 500
    end
  end
  if dice.sort.to_s[/[2]+/]
    if dice.sort.to_s[/[2]+/].length >= 3
      tally += 200
    end
  end
  if dice.sort.to_s[/[3]+/]
    if dice.sort.to_s[/[3]+/].length >= 3
      tally += 300
    end
  end
  if dice.sort.to_s[/[4]+/]
    if dice.sort.to_s[/[4]+/].length >= 3
      tally += 400
    end
  end
  if dice.sort.to_s[/[6]+/]
    if dice.sort.to_s[/[6]+/].length >= 3
      tally += 600
    end
  end
  return tally
end

All test's pass.

非要怀念 2024-12-12 17:27:58

您使用什么 Ruby 版本?我使用的是 1.9.2,我的 IRB 给了我同样的错误,因为如果没有匹配项,正则表达式将返回 nil。
由于 dice = [] 是边界情况,您可以在代码的第一行添加对此的检查,以便返回 0。

我今天做了 RubyKoans,尽管如此,我的代码并不比你的漂亮,也许它会对你有所帮助:

def score(dice)

points = 0
dice.sort!
  nmbrs = Array.new
       dice.each { |n|
               nmbrs[n] = dice.select { |nm| nm == n}
       } 

 n = 0
 nmbrs.each { |vals|
      n = n + 1
      if(vals.nil?)
              next
      end
      if(vals.count >= 3)

              points += (n-1)*100 if (n-1) != 1
              points += 1000 if (n-1) == 1

              if vals.size > 3
                      if (n-1) == 1
                              points += 100 * (vals.size - 3)
                      else
                              if (n-1) == 5
                                      points += 50 * (vals.size - 3)
                            end
                      end

              end
      else
            points += 100 * (vals.count) if (n-1) == 1
            points += 50 * (vals.count) if (n-1) == 5
      end
   }

    points

 end

对于这个蹩脚的函数,我深表歉意,但它确实有效,所以它可能会让你了解如何解决这个问题特别的问题。

祝你好运!

What Ruby version are you using? I'm using 1.9.2 and my IRB gives me the same error since the regexp returns nil if there is no match.
Being dice = [] a border case, you could add a check for that on the first lines of your code in order to return 0 then.

I did the RubyKoans today and even though, my code is not prettier than yours, maybe it'll help you out a bit:

def score(dice)

points = 0
dice.sort!
  nmbrs = Array.new
       dice.each { |n|
               nmbrs[n] = dice.select { |nm| nm == n}
       } 

 n = 0
 nmbrs.each { |vals|
      n = n + 1
      if(vals.nil?)
              next
      end
      if(vals.count >= 3)

              points += (n-1)*100 if (n-1) != 1
              points += 1000 if (n-1) == 1

              if vals.size > 3
                      if (n-1) == 1
                              points += 100 * (vals.size - 3)
                      else
                              if (n-1) == 5
                                      points += 50 * (vals.size - 3)
                            end
                      end

              end
      else
            points += 100 * (vals.count) if (n-1) == 1
            points += 50 * (vals.count) if (n-1) == 5
      end
   }

    points

 end

Sorry about the crappy function, but it does work, so it might give you an idea of how to solve that particular problem.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文