生成随机数时,未初始化为常量

时间:2015-11-07 00:33:56

标签: ruby sum add uninitialized-constant learn-ruby-the-hard-way

我开始为我的学习红宝石做一个小游戏,练习第43号,在我做任何课程之前,我尝试过这样做:

def start
  puts '''Times are tough and we need your help as much as ever.
Plase tell me, what do you do for living? Are you maybe a:
a. swift and strong skater
b. wise and clever lawyer during his first year at University
c. dreamer who only read one book but a good book and a communist commerade in one person'''

  print "> "  
  class = $stdin.gets.chomp.downcase
  if class == "a" || "skater" || "swift and strong skater"

strenght = 5 + Random.rand(12)
dexterity = Random.rand(9...17)
inteligence = Random.rand(7..14)
charisma = 2 + Random.rand(10)

  puts '''Your stats are:
          STRENGHT: #{strenght}
          DEXTERITY #{dexterity}
          INTELIGENCE #{inteligence}
          CHARISMA #{charisma}
          Good job! '''
          armory_room
  end
end

它没有用,我得到了:“未初始化的恒定”按摩。我也试过这个:

charisma = 2 + Random.rand(10)
puts "Pls work #{charisma}"

我再次得到了“未初始化的”按摩。为什么它不起作用,我应该如何使它工作?

2 个答案:

答案 0 :(得分:0)

class是保留名称。您不能将其用作变量名称。 请尝试使用class_。

答案 1 :(得分:0)

在ruby中,class是保留关键字。最佳做法是使用

  

克拉斯