Search code examples
rubynomethoderror

Ruby Koans Scoring Project. undefined method `length' for nil:NilClass


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?


Solution

  • 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.