Can I build an array using a loop in Ruby? -
i started learning ruby/rails, , trying write program builds array, formats new array.
it works second while
, and, if have array built, second part works well. there leaving out?
chap = [] page = [] linewidth = 80 x = 0 n = chap.length.to_i puts 'chapter?' chapter = gets.chomp while chapter != '' chap.push chapter puts 'page?' pg = gets.chomp page.push pg puts 'chapter?' chapter = gets.chomp end puts ('table of contents').center linewidth puts '' while x < n puts ('chapter ' + (x+1).to_s + ' ' + chap[x]).ljust(linewidth/2) +(' page ' + page[x]).rjust(linewidth/2) x = x + 1 end
thanks help!
simple pilot error: called
n = chap.length.to_i
too early. have length of chap list after put things in it. move line here:
... puts 'chapter?' chapter = gets.chomp end n = chap.length.to_i puts ('table of contents').center linewidth
and works fine.
Comments
Post a Comment