First Lesson: Drawing a diamond with Ruby
I was reminiscing recently about what my programming exercises were at high school. I started on Turbo Pascal.
This is was one of the first, simply to draw a diamond shape, that looked like this:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
I remember it taking forever, but loving it. So decided to write it out in ruby.
s = 10
(-s..s+1).each do |y|
(-s..s+1).each do |x|
print x.abs + y.abs > s ? ' ' : '*'
end
puts
end
Alternative solution:
s = 10
s.times {|u| puts ' ' * (s - u.to_i) + '*' * (u + u.to_i) }
s.downto(0).each {|u| puts ' ' * (s - u.to_i) + '*' * (u + u.to_i) };