For my next trick I’m trying to understand how to do stuff with
classes and objects. Naturally, this leads me to game stuff.
The file Character.rb has the class definition. The file
trav_functions.rb has the functions I’m using and the run file is
chargen.rb.
The error I’m getting is:
./chargen.rb
My age is 34 and my UPP is 787878.
./Character.rb:19:in set_stat': undefined method[]=’ for
nil:NilClass (NoMethodError)
from ./chargen.rb:17
from ./chargen.rb:16:in `each’
from ./chargen.rb:16
I’m setting the stats hash in the Character.rb file. ‘set_stat’ is a
method:
@stats = Hash.new
@stats = {
‘Str’ => nil,
‘Dex’ => nil,
‘End’ => nil,
‘Int’ => nil,
‘Edu’ => nil,
‘Soc’ => nil
}
def set_stat(stat, num)
@stats[stat] = num
end
I’m pretty sure this is operator error, just not sure what the issue
is. Thoughts?
Leam
######################
Character.rb
class Character
def initialize(gender, career, skills)
@gender = gender
@career = career
@skills = skills
end
@stats = Hash.new
@stats = {
‘Str’ => nil,
‘Dex’ => nil,
‘End’ => nil,
‘Int’ => nil,
‘Edu’ => nil,
‘Soc’ => nil
}
def set_stat(stat, num)
@stats[stat] = num
end
def get_stat(stat)
return @stats[stat]
end
def set_terms(terms)
@terms = terms
end
def get_terms
return @terms
end
def set_age
@age = (@terms * 4) + 18
end
def get_age
return @age
end
def set_upp(upp)
@upp = upp
end
def get_upp
return @upp
end
end
chargen.rb
#!/usr/bin/env ruby
require ‘Character’
require ‘trav_functions.rb’
me = Character.new(‘m’, ‘Scout’, ‘scuba’)
me.set_terms(4)
me.set_age
me.set_upp(‘787878’)
my_age = me.get_age
my_upp = me.get_upp
puts “My age is #{my_age} and my UPP is #{my_upp}.”
stats_names = [‘Str’, ‘Dex’, ‘End’, ‘Int’, ‘Edu’, ‘Soc’]
stats_names.each do |stat|
me.set_stat(stat, roll2)
end
trav_functions.rb
#!/usr/bin/env ruby
def roll2
return 2 + rand(6) + rand(6)
end
def hexconvert(num)
return num.to_s(16).upcase
end
def make_stat
return hexconvert(roll2)
end
def set_career
terms = rand(6) + 1
age = 18 + (terms * 4)
careers = [‘Navy’, ‘Army’, ‘Marines’, ‘Merchants’, ‘Scouts’]
career = careers[rand(careers.length)]
return age, terms, career
end
def build_skills(career)
skills = Hash.new
skills[‘Army’] = [‘CbtR’, ‘Drive’, ‘Ldr’, ‘CbtR’, ‘Mech’, ‘GunCbt’, ]
skills[‘Marines’] = [‘CbtR’, ‘BattleDress’, ‘Ldr’, ‘CbtR’, ‘Mech’,
‘GunCbt’, ]
skills[‘Navy’] = [‘Pilot’, ‘Nav’, ‘Sensors’,]
skills[‘Merchants’] = [‘Pilot’, ‘Nav’, ‘Broker’, ‘Steward’, ‘Math’]
skills[‘Scouts’] = [‘Pilot’, ‘Nav’, ‘Sensors’,]
max = skills[career].length
return skills[career][rand(max)]
end
#stats = {
‘Str’ => nil,
‘Dex’ => nil,
‘End’ => nil,
‘Int’ => nil,
‘Edu’ => nil,
‘Soc’ => nil
}
#stats_names = [‘Str’, ‘Dex’, ‘End’, ‘Int’, ‘Edu’, ‘Soc’]
#upp = ‘’
#stats.each_key do |stat|
stats[stat] = make_stat
#end
age, terms, career = set_career
skill_list = Hash.new
terms.times do
new_skill = build_skills(career)
skill_list.[new_skill]=0
end
Output
#puts “.” * 10
#stats_names.each do |stat|
upp = upp + stats[stat]
#end
#puts “UPP: #{upp}.”
#puts “#{age} year old with #{terms} terms in the #{career}.”
#skill_list.each do |skill|
puts skill
#end