Hey all, my first post.
ruby 1.8.7 on RHEL 6.
Trying to build a hash, populate it, and then print it in a particular
order. The output is not what I’m looking for. Some hash keys look odd
and some puts statements fail. Since I’m new to Ruby my guess is
“operator error”. Would appreciate being pointed in the right
direction. Sample output is below the code.
#!/usr/bin/env ruby
def roll2
return 2 + rand(6) + rand(6)
end
def hexconvert(num)
if num > 9
makecap = true
end
num = num.to_s(16)
if makecap
num = num.capitalize
end
return num
end
def make_stat
roll = roll2
return hexconvert(roll)
end
stats = {
‘Str’ => nil,
‘Dex’ => nil,
‘End’ => nil,
‘Int’ => nil,
‘Edu’ => nil,
‘Soc’ => nil
}
stats.each do |stat|
stats[stat] = make_stat
puts “#{stat} is #{stats[stat]}.”
end
puts “.” * 10
stats_names = [‘Str’, ‘Dex’, ‘End’, ‘Int’, ‘Edu’, ‘Soc’]
stats_names.each { |stat| puts “#{stat} is #{stats[stat]}.”}
puts “.” * 10
Sample output
./exercises.rb
Soc is 3.
Int is 8.
End is A.
Dex is 3.
Dex3 is 4.
Str is 7.
Soc3 is 8.
Soc38 is 7.
Edu is 5.
…
Str is .
Dex is .
End is .
Int is .
Edu is .
Soc is .
…
Desired output
Str is 7
Dex is 3
End is A
Int is 8
Edu is 5
Soc is 7