# \[QUIZ\] Second answer for quiz 84

**URL:** <https://www.ruby-forum.com/t/quiz-second-answer-for-quiz-84/96515>\
**Category:** Ruby\
**Created:** [June 29, 2006, 5:14am UTC](https://www.ruby-forum.com/t/quiz-second-answer-for-quiz-84/96515 "2006-06-29T05:14:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [June 29, 2006, 5:14am UTC](https://www.ruby-forum.com/t/quiz-second-answer-for-quiz-84/96515/1 "2006-06-29T05:14:16Z")

</div>

After looking over my earlier answer, I realized that I had re-invented  
String.center and had a number of excesses. This one is a lot smaller  
but I  
still feel I’m not using the language to its fullest.

JB

# pp\_pascal.rb

# for details, see [Ruby Quiz - pp Pascal (#84)](http://www.rubyquiz.com/quiz84.html)

class Pascal  
def initialize( depth )  
@d = depth  
@p = [[1]]  
1.upto(@d-1) do |row|  
@p[row] = [1]  
1.upto(row-1) do |col|  
@p[row] \<\< (@p[row-1][col-1] + @p[row-1][col])  
end  
@p[row] \<\< 1  
end  
end  
def show  
# the middle entry in the bottom row will have the  
# most digits - use its length as the per-entry span  
span = (@p[-1][(@d / 2).to\_i].to\_s.length) \* 2  
width = @d \* span  
rows = []  
@p.each do |p|  
ret = ‘’  
p.each do |i|  
ret += i.to\_s.center(span)  
end  
rows \<\< ret.center(width)  
end  
rows.join(“\n”)  
end  
end

# default to 10 rows

rows = (ARGV[0] || 10).to\_i  
p = Pascal.new(rows)  
puts p.show
