How to use Array in Ruby? -
i newbie in ruby. trying write simple interactive game in ruby stuck. need proceed. below concept:
money = [100, 200, 300, 400] beneficiaries = ["john", "sam", "mary"] cost1 = [50, 25, 30, 75, 18] cost2 = [120, 150, 200, 250] gift1 = [" crayon ", " pencil ", " biro "] gift2 = [" bag ", " shoe ", " radio "] cashowned = [50] = money.sample b = cost1.sample c = cost2.sample d = gift1.sample e = gift2.sample f = cashowned puts " hi, name? " puts name = gets.chomp puts puts " #{name} nice name." puts puts "#{name} have #{f} in bank account." puts puts "roll dice , let's see how earned." puts gets.chomp.to_i dice1 = puts "#{name} earned: #{dice1}" puts gets.chomp.to_i cashowned = dice1 + f puts "your account balance : #{cashowned}" puts gets.chomp.to_i
i stuck here. want repeat/loop sequence can give out gift @ cost , deduct cash owned. cash owned not updating. simpler way go appreciated. thanks.
ruby case sensitive language.
fist declared variable as:
cashowned = [50]
and later trying update using variable name:
cashowned = dice1 + f
note "cashowned"
, "cashowned"
different variables
update:
line 7, have:
cashowned = [50]
my question is, why not use only:
cashowned = 50
?
as doing cause error: array can't coerced fixnum (typeerror)
you trying sum fixnum array.
see code:
money = [100, 200, 300, 400] beneficiaries = ["john", "sam", "mary"] cost1 = [50, 25, 30, 75, 18] cost2 = [120, 150, 200, 250] gift1 = ["crayon", "pencil", "biro"] gift2 = ["bag", "shoe", "radio"] cashowned = 50 = money.sample b = cost1.sample c = cost2.sample d = gift1.sample e = gift2.sample f = cashowned puts "hi, name?" name = gets.chomp puts "#{name} nice name." puts "#{name} have #{f} in bank account." puts "roll dice , let's see how earned." gets.chomp.to_i dice1 = puts "#{name} earned: #{dice1}" gets.chomp.to_i cashowned = dice1 + f puts "your account balance : #{cashowned}" gets.chomp.to_i
and see test result:
Comments
Post a Comment