用 for 循环打印一个三角形
triangle = <<EOF
*
* *
* *
* * * *
EOF
for line in triangle.lines
puts line
end
max_bottom_edge = (ARGV[0].chomp.to_i rescue 3)
# only support for odd number
if max_bottom_edge & 1 == 0
max_bottom_edge += 1
end
level = (max_bottom_edge +1) / 2
for i in 1..level
# spaces
(level-i).times do
print(" ")
end
(i*2-1).times do
print("*")
end
print("\n")
end
=begin
$ ruby main.rb 29
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
=end