1

A rather unexpected error "NoMethodError: undefined method '-@' for ["some-text"]:Array" for a very simple piece of code:

t = []
times = 1
while times > 0 do
    times--

    case rand(16)+1
    when  1 then t.push("human-like")
    when  2 then t.push("amoeboid")
    when  3 then t.push("beast")
    when  4 then t.push("aerial")
    when  5 then t.push("amphibian")
    when  6 then t.push("bipedal")
    when  7 then t.push("insectoid")
    when  8 then t.push("radially symmetrical")
    when  9 then t.push("multipedal")
    when 10 then t.push("piscean")
    when 11 then t.push("reptilian")
    when 12 then t.push("humanoid")
    when 13 then t.push("cetacean")
    when 14 then t.push("botanic")
    when 15 then t.push("non-organic")
    else times += 2
    end
end

Simply stating:

t = []
t.push("whatever")

...works as expected, but the part inside while loop fails miserably. I can't wrap my mind around why. Any clues? What I'm blind to?

Scre
  • 454
  • 1
  • 10
  • 15
  • 2
    Executive summary: `times--` doesn't make sense in Ruby as Ruby doesn't have a post-decrement operator. Ruby is probably interpreting your code as `times - -(case ... end)` and thus trying to apply the unary negation (which is the `-@` method) to what `t.push` returns. Use `times -= 1` instead. – mu is too short Nov 05 '16 at 02:29
  • *facepalm* ... so that's why. Thanks for pointing the error! So very used to using -- and ++ ... – Scre Nov 05 '16 at 02:33

0 Answers0