Print Pyramid Patterns with Ruby Language

Here is a quick approach to print pytamid patterns with Ruby Language. It’s simple and easy approach that anyone can easily understand and try on their own. I am taking rows value in every for-in loop but I have just mentioned once on the very first code block.

#1

print "Enter Number for Rows", ": "

rows = gets.chomp.to_i

for row in 0..rows
	row.times {print "*"}
	puts
end
*
**
***
****
*****

#2

for row in 0..rows
	(rows-row).times {print "*"}
	puts
end
*****
****
***
**
*

#3

for row in 0..rows
	(rows-row).times {print " "}
	row.times {print "*"}
	puts
end
    *
   **
  ***
 ****
*****

#4

for row in 0..rows
	row.times {print " "}
      	(rows-row).times {print "*"}
	puts
end
*****
 ****
  ***
   **
    *

#5

for row in 0..rows
	(rows-row).times {print " "}
	row.times {print "*"}
	(row-1).times {print "*" }
	puts
end
    *
   ***
  *****
 *******
*********

#6

for row in 0..rows
	row.times {print " "}
	(rows-row).times {print "*" }
	(rows-row-1).times {print "*"}
	puts
end
*********
 *******
  *****
   ***
    *

#7

for row in 0..rows
	(rows-row).times {print " "}
	row.times {print "*"}
	(row-1).times {print "*" }
	puts
end

for row in 0..rows
	row.times {print " "}
	(rows-row).times {print "*" }
	(rows-row-1).times {print "*"}
	puts
end
    *
   ***
  *****
 *******
*********
*********
 *******
  *****
   ***
    *

Here is all the patters mentioned above in one ruby file. If you would like, you can copy the code and run it on your system.

print "Enter Number for Rows", ": "

rows = gets.chomp.to_i

for row in 0..rows
	row.times {print "*"}
	puts
end


puts "-------------------"

# decreasing pattern
for row in 0..rows
	(rows-row).times {print "*"}
	puts
end


puts "-------------------"

for row in 0..rows
	(rows-row).times {print " "}
	row.times {print "*"}
	puts
end

puts "-------------------------"
for row in 0..rows
	row.times {print " "}
      	(rows-row).times {print "*"}
	puts
end

puts '------------------------'
for row in 0..rows
	(rows-row).times {print " "}
	row.times {print "*"}
	(row-1).times {print "*" }
	puts
end

puts "=========================="

for row in 0..rows
	row.times {print " "}
	(rows-row).times {print "*" }
	(rows-row-1).times {print "*"}
	puts
end

# these two FOR loops run togather
for row in 0..rows
	(rows-row).times {print " "}
	row.times {print "*"}
	(row-1).times {print "*" }
	puts
end

for row in 0..rows
	row.times {print " "}
	(rows-row).times {print "*" }
	(rows-row-1).times {print "*"}
	puts
end